From 63e948fce111e774297ac7c9fbe0a5d1c6327141 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (VMware)" Date: Thu, 25 Oct 2018 12:50:15 +0100 Subject: [PATCH] Add sample function Signed-off-by: Alex Ellis (VMware) --- .../business-strategy-generator/handler.go | 14 +++ .../nishakm/strategy_generator/README.md | 9 ++ .../nishakm/strategy_generator/main.go | 24 ++++ .../strategy_generator/pkg/generator.go | 111 ++++++++++++++++++ sample-functions/stack.yml | 5 +- 5 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 sample-functions/business-strategy-generator/handler.go create mode 100644 sample-functions/business-strategy-generator/vendor/github.com/nishakm/strategy_generator/README.md create mode 100644 sample-functions/business-strategy-generator/vendor/github.com/nishakm/strategy_generator/main.go create mode 100644 sample-functions/business-strategy-generator/vendor/github.com/nishakm/strategy_generator/pkg/generator.go diff --git a/sample-functions/business-strategy-generator/handler.go b/sample-functions/business-strategy-generator/handler.go new file mode 100644 index 00000000..125b4e91 --- /dev/null +++ b/sample-functions/business-strategy-generator/handler.go @@ -0,0 +1,14 @@ +package function + +import ( + "fmt" + + "github.com/nishakm/strategy_generator/pkg" +) + +// Handle a serverless request +func Handle(req []byte) string { + + statement := pkg.Generate() + return fmt.Sprintf("%s", statement) +} diff --git a/sample-functions/business-strategy-generator/vendor/github.com/nishakm/strategy_generator/README.md b/sample-functions/business-strategy-generator/vendor/github.com/nishakm/strategy_generator/README.md new file mode 100644 index 00000000..9a6fd31d --- /dev/null +++ b/sample-functions/business-strategy-generator/vendor/github.com/nishakm/strategy_generator/README.md @@ -0,0 +1,9 @@ +# Business Strategy Generator written in Go + +This was inspired by [a blog post by Simon Wardley](http://blog.gardeviance.org/2014/07/a-quick-route-to-building-strategy.html) + +It also doubles as a Go programming guide and hence has more comments than actual lines of code + +Content in the code is governed by the [Creative Commons Attribution-Share Alike 3.0 License](http://creativecommons.org/licenses/by-sa/3.0/) + +It's a silly project but you can submit PRs to it if you'd like diff --git a/sample-functions/business-strategy-generator/vendor/github.com/nishakm/strategy_generator/main.go b/sample-functions/business-strategy-generator/vendor/github.com/nishakm/strategy_generator/main.go new file mode 100644 index 00000000..42bf5c66 --- /dev/null +++ b/sample-functions/business-strategy-generator/vendor/github.com/nishakm/strategy_generator/main.go @@ -0,0 +1,24 @@ +// Business Strategy Generator written in Go +// Inspired by http://blog.gardeviance.org/2014/07/a-quick-route-to-building-strategy.html +// Doubles as a learning guide for Go (so there are more comments than actual lines of code) +// Copyright (c) Nisha Kumar (@nishakm) All Rights Reserved +// SPDX-License-Identifier: BSD-2-Clause + +// Entry point of any Go program is the main package +// The file name can be called anything +// I am calling it 'main.go' for clarity +package main + +// Import other golang packages here. Names are in "" +import ( + "fmt" + + "github.com/nishakm/strategy_generator/pkg" +) + +// this is the main function +func main() { + statement := pkg.Generate() + + fmt.Println(statement) +} diff --git a/sample-functions/business-strategy-generator/vendor/github.com/nishakm/strategy_generator/pkg/generator.go b/sample-functions/business-strategy-generator/vendor/github.com/nishakm/strategy_generator/pkg/generator.go new file mode 100644 index 00000000..11af576b --- /dev/null +++ b/sample-functions/business-strategy-generator/vendor/github.com/nishakm/strategy_generator/pkg/generator.go @@ -0,0 +1,111 @@ +package pkg + +import ( + "fmt" + "math/rand" + "time" +) + +// Statement is a struct and something to encapsulate the +// string and the type of word that gets filled in +// note that we are using Capital letters for the variables +// this is so we can access them directly +type Statement struct { + Sentence string // the sentence to substitute a word in + WordType int // 0 for noun and 1 for adjective (more word organization needed! Pull Requests accepted :) +} + +// nouns is an array assignment with no specific limits +// that means other words can be added here +var nouns = []string{ + "efficiency", + "competitive advantage", + "ecosystems", + "synergy", + "learning organization", + "network", + "social media", + "revolution", + "big data", + "security", + "internet of things", + "digital business", + "data leaders", + "big data", + "insight from data", + "platform", + "culture"} + +var adjectives = []string{ + "digital first", + "agile", + "open", + "innovative", + "networked", + "collaborative", + "cloud based", + "growth focused", + "secure", + "customer focused", + "disruptive", + "platform based", + "sustainable", + "value added"} + +// Each statement has a %s which is for string substitution +// in Go there is no tuple substitution like python so the +// whole statement needs to be divided in this way +var statements = []Statement{ + Statement{"Our strategy is %s. ", 1}, + Statement{"We will lead a %s ", 1}, + Statement{"effort of the market through our use of %s ", 0}, + Statement{"and %s ", 0}, + Statement{"to build a %s. ", 0}, + Statement{"By being both %s ", 1}, + Statement{"and %s, ", 1}, + Statement{"our %s ", 1}, + Statement{"approach will drive %s throughout the organization. ", 0}, + Statement{"Synergies between our %s ", 0}, + Statement{"and %s ", 0}, + Statement{"will enable us to capture the upside by becoming %s ", 1}, + Statement{"in a %s world. ", 0}, + Statement{"These transformations combined with %s ", 0}, + Statement{"due to our %s ", 0}, + Statement{"will create a %s ", 0}, + Statement{"through %s ", 0}, + Statement{"and %s.", 0}} + +// Generate produces a random business statedgy statement. +// Note: any of the functions called from the packages imported +// has to start with a Capital letter this is because of the rule +// that only functions starting with a capital letter are 'exported' +// so you can call them here from another package. +// It's not really the same as public and private but more like direct +// and indirect reference or use +// https://www.ardanlabs.com/blog/2014/03/exportedunexported-identifiers-in-go.html +func Generate() string { + + // implicit variable assignment + // not available outside the function + statement := "" + + // explicit unassigned variable + var word string + + // rand uses a global package called Source + // https://golang.org/pkg/math/rand/#Source + // The source has to be initialized by some seed + // the seed needs to change every time a random number needs to + // be generated or else the same number will be generated + for i := 0; i < len(statements); i++ { + rand.Seed(time.Now().UnixNano()) + if statements[i].WordType == 0 { + word = nouns[rand.Intn(len(nouns))] + } else { //like C else has to be right after the closing } + word = adjectives[rand.Intn(len(adjectives))] + } + statement = statement + fmt.Sprintf(statements[i].Sentence, word) + } + + return statement +} diff --git a/sample-functions/stack.yml b/sample-functions/stack.yml index 7f524583..34906a7c 100644 --- a/sample-functions/stack.yml +++ b/sample-functions/stack.yml @@ -86,4 +86,7 @@ functions: lang: dockerfile handler: ./WordCountFunction image: functions/wordcount:latest - + business-strategy-generator: + lang: go + handler: ./business-strategy-generator + image: functions/business-strategy-generator:0.1