Add sample function

Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (VMware) 2018-10-25 12:50:15 +01:00 committed by Alex Ellis
parent f5939c9a60
commit 63e948fce1
5 changed files with 162 additions and 1 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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
}

View File

@ -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