├── .github └── workflows │ ├── github-actions.yml │ └── golangci-lint.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE ├── Makefile ├── NOTICE ├── README.md ├── README_CN.md ├── aop ├── aop.go ├── boot.go ├── common │ ├── config.go │ ├── filed_matcher.go │ ├── listener.go │ ├── metadata.go │ ├── runtime.go │ ├── test.go │ └── util.go ├── invocation_context.go ├── metadata.go ├── proxy.go └── server.go ├── autowire ├── alias.go ├── autowire_mock.go ├── base │ ├── base.go │ └── base_test.go ├── boot.go ├── implements.go ├── metadata.go ├── mocks │ ├── FacadeAutowire.go │ ├── ParamLoader.go │ ├── SDIDParser.go │ └── WrapperAutowire.go ├── model.go ├── normal │ ├── normal.go │ └── normal_test.go ├── param_loader │ ├── default.go │ ├── default_config.go │ ├── default_tag.go │ └── default_tag_point_to_config.go ├── proxy.go ├── sdid_parser │ ├── sdid_parser.go │ └── sdid_parser_test.go ├── singleton │ ├── singleton.go │ └── singleton_test.go ├── struct_descriptor.go ├── util │ ├── util.go │ └── util_test.go ├── wrapper.go └── wrapper_test.go ├── boot.go ├── config ├── boot.go ├── boot_option_test.go ├── boot_test.go ├── expand.go ├── file.go ├── file_test.go ├── merge_map.go ├── merge_map_test.go ├── regepx.go ├── regepx_test.go └── test │ ├── ioc_golang-bad-config.yaml │ ├── ioc_golang.yaml │ └── ioc_golang_dev.yaml ├── example ├── README.md ├── README_EN.md ├── aop │ ├── README.md │ ├── call │ │ ├── README.md │ │ ├── dto │ │ │ └── user.go │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ ├── dynamic_plugin │ │ ├── complex │ │ │ ├── config │ │ │ │ └── config.yaml │ │ │ ├── docker-compose │ │ │ │ └── docker-compose.yaml │ │ │ ├── main.go │ │ │ ├── main_test.go │ │ │ ├── plugin_1 │ │ │ │ ├── service.go │ │ │ │ └── zz_generated.ioc.go │ │ │ ├── service1 │ │ │ │ ├── service.go │ │ │ │ └── zz_generated.ioc.go │ │ │ ├── service2 │ │ │ │ ├── service.go │ │ │ │ └── zz_generated.ioc.go │ │ │ └── zz_generated.ioc.go │ │ └── simple │ │ │ ├── main.go │ │ │ ├── main_test.go │ │ │ ├── plugin_1 │ │ │ └── service_plugin.go │ │ │ └── zz_generated.ioc.go │ ├── log │ │ ├── README.md │ │ ├── app │ │ │ ├── app.go │ │ │ └── zz_generated.ioc.go │ │ ├── main.go │ │ └── test_file │ │ │ ├── test_configure │ │ │ └── main_test.go │ │ │ ├── test_default │ │ │ └── main_test.go │ │ │ └── test_global_logger_readonly │ │ │ └── main_test.go │ ├── observability │ │ ├── README.md │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ └── transaction │ │ ├── README.md │ │ ├── distributed │ │ ├── client │ │ │ ├── main.go │ │ │ ├── main_test.go │ │ │ └── zz_generated.ioc.go │ │ └── server │ │ │ ├── main.go │ │ │ └── pkg │ │ │ └── service │ │ │ ├── api │ │ │ └── zz_generated.ioc_rpc_client_bankservice.go │ │ │ ├── service.go │ │ │ └── zz_generated.ioc.go │ │ └── singleton │ │ ├── cmd │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ │ └── service │ │ ├── bank.go │ │ ├── trade.go │ │ └── zz_generated.ioc.go ├── autowire │ ├── autowire_active_profile_implements │ │ ├── main.go │ │ ├── main_test.go │ │ ├── service │ │ │ ├── impl │ │ │ │ ├── default.go │ │ │ │ ├── dev.go │ │ │ │ ├── pro.go │ │ │ │ └── zz_generated.ioc.go │ │ │ └── service.go │ │ └── zz_generated.ioc.go │ ├── autowire_allimpls │ │ ├── main.go │ │ ├── main_test.go │ │ ├── service │ │ │ ├── impl │ │ │ │ ├── impl1.go │ │ │ │ ├── impl2.go │ │ │ │ └── zz_generated.ioc.go │ │ │ ├── impl3.go │ │ │ ├── impl4.go │ │ │ ├── service.go │ │ │ └── zz_generated.ioc.go │ │ └── zz_generated.ioc.go │ ├── autowire_config │ │ ├── README.md │ │ ├── cmd │ │ │ ├── main.go │ │ │ ├── main_test.go │ │ │ └── zz_generated.ioc.go │ │ └── conf │ │ │ └── config.yaml │ ├── autowire_rpc │ │ ├── README.md │ │ ├── client │ │ │ ├── main.go │ │ │ ├── main_test.go │ │ │ ├── test │ │ │ │ ├── client_test.go │ │ │ │ ├── dto │ │ │ │ │ ├── custom.go │ │ │ │ │ └── user.go │ │ │ │ └── service │ │ │ │ │ ├── api │ │ │ │ │ ├── zz_generated.ioc_rpc_client_complexservice.go │ │ │ │ │ └── zz_generated.ioc_rpc_client_simpleservice.go │ │ │ │ │ ├── complex.go │ │ │ │ │ ├── simple.go │ │ │ │ │ └── zz_generated.ioc.go │ │ │ └── zz_generated.ioc.go │ │ └── server │ │ │ ├── main.go │ │ │ └── pkg │ │ │ ├── dto │ │ │ └── user.go │ │ │ └── service │ │ │ ├── api │ │ │ └── zz_generated.ioc_rpc_client_servicestruct.go │ │ │ ├── service.go │ │ │ └── zz_generated.ioc.go │ └── get_impl_by_api │ │ ├── README.md │ │ ├── cmd │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ │ └── docker-compose │ │ └── docker-compose.yaml ├── config_file │ ├── README.md │ ├── active_profile │ │ ├── config │ │ │ ├── config_dev.yaml │ │ │ └── config_pro.yaml │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ ├── complex_example │ │ ├── docker-compose │ │ │ └── docker-compose.yaml │ │ ├── main.go │ │ ├── main_test.go │ │ ├── my_custom_config_path │ │ │ ├── config_dev.yaml │ │ │ └── config_pro.yaml │ │ ├── redis_config │ │ │ ├── config_dev.yaml │ │ │ └── config_pro.yaml │ │ └── zz_generated.ioc.go │ ├── config_by_api │ │ ├── docker-compose │ │ │ └── docker-compose.yaml │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ ├── default_config_file │ │ ├── config │ │ │ └── config.yaml │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ ├── mark_env_variable_in_config_file │ │ ├── config │ │ │ └── config.yaml │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ ├── mark_nested_value_in_config_file │ │ ├── config │ │ │ └── config.yaml │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ ├── set_config_file_search_path │ │ ├── config │ │ │ └── config.yaml │ │ ├── custom_config_path │ │ │ └── config.yaml │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ ├── set_config_file_type │ │ ├── config │ │ │ ├── config.yaml │ │ │ └── config.yml │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ └── set_config_name │ │ ├── config │ │ ├── config.yaml │ │ └── custom_config.yaml │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go ├── helloworld │ ├── README_CN.md │ ├── main.go │ └── zz_generated.ioc.go └── third_party │ ├── autowire │ └── grpc │ │ ├── README.md │ │ ├── api │ │ ├── hello.pb.go │ │ ├── hello.proto │ │ └── hello_grpc.pb.go │ │ ├── cmd │ │ ├── main.go │ │ ├── main_test.go │ │ ├── service1 │ │ │ ├── service1_impl1.go │ │ │ ├── service1_interface.go │ │ │ └── zz_generated.ioc.go │ │ ├── service2 │ │ │ ├── service2_impl1.go │ │ │ ├── service2_impl2.go │ │ │ ├── service2_interface.go │ │ │ └── zz_generated.ioc.go │ │ ├── struct1 │ │ │ ├── service1_impl1.go │ │ │ └── zz_generated.ioc.go │ │ └── zz_generated.ioc.go │ │ ├── conf │ │ └── config.yaml │ │ ├── docker-compose │ │ └── docker-compose.yaml │ │ └── grpc_server │ │ ├── Dockerfile │ │ ├── Makefile │ │ └── server.go │ ├── db │ └── gorm │ │ ├── README.md │ │ ├── cmd │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ │ ├── conf │ │ └── config.yaml │ │ └── docker-compose │ │ └── docker-compose.yaml │ ├── pubsub │ └── rocketmq │ │ ├── cmd │ │ ├── main.go │ │ └── zz_generated.ioc.go │ │ └── conf │ │ └── config.yaml │ ├── registry │ └── nacos │ │ ├── cmd │ │ ├── main.go │ │ ├── main_test.go │ │ └── zz_generated.ioc.go │ │ ├── conf │ │ └── config.yaml │ │ └── docker-compose │ │ └── docker-compose.yaml │ └── state │ └── redis │ ├── README.md │ ├── cmd │ ├── main.go │ ├── main_test.go │ └── zz_generated.ioc.go │ ├── conf │ └── config.yaml │ └── docker-compose │ └── docker-compose.yaml ├── extension ├── README.md ├── README_EN.md ├── aop │ ├── call │ │ ├── aop.go │ │ ├── api │ │ │ └── ioc_golang │ │ │ │ └── aop │ │ │ │ └── call │ │ │ │ ├── call.pb.go │ │ │ │ ├── call.proto │ │ │ │ └── call_grpc.pb.go │ │ ├── cli │ │ │ └── call.go │ │ ├── server.go │ │ └── zz_generated.ioc.go │ ├── dynamic_plugin │ │ ├── aop.go │ │ ├── api │ │ │ └── ioc_golang │ │ │ │ └── aop │ │ │ │ └── dynamic_plugin │ │ │ │ ├── dynamic_plugin.pb.go │ │ │ │ ├── dynamic_plugin.proto │ │ │ │ └── dynamic_plugin_grpc.pb.go │ │ ├── cli │ │ │ ├── dynaimc_plugin.go │ │ │ └── update.go │ │ ├── server.go │ │ └── zz_generated.ioc.go │ ├── list │ │ ├── aop.go │ │ ├── api │ │ │ └── ioc_golang │ │ │ │ └── aop │ │ │ │ └── list │ │ │ │ ├── list.pb.go │ │ │ │ ├── list.proto │ │ │ │ └── list_grpc.pb.go │ │ ├── cli │ │ │ └── list.go │ │ ├── server.go │ │ ├── server_test.go │ │ ├── sorter.go │ │ ├── sorter_test.go │ │ └── zz_generated.ioc.go │ ├── log │ │ ├── aop.go │ │ ├── api │ │ │ └── ioc_golang │ │ │ │ └── aop │ │ │ │ └── log │ │ │ │ ├── log.pb.go │ │ │ │ ├── log.proto │ │ │ │ └── log_grpc.pb.go │ │ ├── cli │ │ │ └── log.go │ │ ├── config.go │ │ ├── context.go │ │ ├── global_logrus_hook.go │ │ ├── interceptor.go │ │ ├── invocation_ctx_logs_generator.go │ │ ├── server.go │ │ ├── test │ │ │ └── dynamic_change_log_test.go │ │ └── zz_generated.ioc.go │ ├── monitor │ │ ├── aop.go │ │ ├── api │ │ │ └── ioc_golang │ │ │ │ └── aop │ │ │ │ └── monitor │ │ │ │ ├── monitor.pb.go │ │ │ │ ├── monitor.proto │ │ │ │ └── monitor_grpc.pb.go │ │ ├── cli │ │ │ └── monitor.go │ │ ├── context.go │ │ ├── interceptor.go │ │ ├── interceptor_mock.go │ │ ├── server.go │ │ ├── server_test.go │ │ ├── sorter.go │ │ ├── sorter_test.go │ │ ├── util.go │ │ └── zz_generated.ioc.go │ ├── trace │ │ ├── aop.go │ │ ├── api │ │ │ ├── ioc_golang │ │ │ │ └── aop │ │ │ │ │ └── trace │ │ │ │ │ ├── trace.pb.go │ │ │ │ │ ├── trace.proto │ │ │ │ │ └── trace_grpc.pb.go │ │ │ └── jaeger │ │ │ │ └── jaeger_model.proto │ │ ├── cli │ │ │ └── trace.go │ │ ├── common │ │ │ ├── jaeger_endpoint.go │ │ │ └── span_key.go │ │ ├── config.go │ │ ├── debug_server_context.go │ │ ├── goroutine_trace │ │ │ ├── context.go │ │ │ ├── interceptor.go │ │ │ └── zz_generated.ioc.go │ │ ├── interceptor.go │ │ ├── log │ │ │ ├── extension │ │ │ │ ├── log.go │ │ │ │ ├── log15.go │ │ │ │ ├── logrus.go │ │ │ │ └── zz_generated.ioc.go │ │ │ ├── interceptor.go │ │ │ └── wrapperWriter.go │ │ ├── rpc_interceptor.go │ │ ├── server.go │ │ ├── sorter.go │ │ ├── sorter_test.go │ │ ├── span_with_parent.go │ │ ├── trace.go │ │ ├── tracer.go │ │ ├── transport │ │ │ ├── collector.go │ │ │ └── wrapper.go │ │ └── zz_generated.ioc.go │ ├── transaction │ │ ├── aop.go │ │ ├── cli │ │ │ ├── gen.go │ │ │ ├── plugin.go │ │ │ └── zz_generated.ioc.go │ │ ├── context.go │ │ ├── context_mock.go │ │ ├── interceptor.go │ │ ├── interceptor_test.go │ │ ├── metadata.go │ │ ├── metadata_test.go │ │ └── zz_generated.ioc.go │ └── watch │ │ ├── aop.go │ │ ├── api │ │ └── ioc_golang │ │ │ └── aop │ │ │ └── watch │ │ │ ├── watch.pb.go │ │ │ ├── watch.proto │ │ │ └── watch_grpc.pb.go │ │ ├── cli │ │ └── watch.go │ │ ├── interceptor.go │ │ ├── interceptor_test.go │ │ ├── server.go │ │ └── zz_generated.ioc.go ├── autowire │ ├── allimpls │ │ ├── allimpls.go │ │ ├── allimpls_test.go │ │ ├── cli │ │ │ ├── code_generator_plugin.go │ │ │ ├── marker.go │ │ │ └── zz_generated.ioc.go │ │ ├── metadata.go │ │ └── sdid_parser.go │ ├── common │ │ ├── cli │ │ │ ├── code_generator_plugin.go │ │ │ ├── marker.go │ │ │ └── zz_generated.ioc.go │ │ └── util.go │ ├── config │ │ ├── config.go │ │ ├── config_test.go │ │ └── param_loader.go │ ├── grpc │ │ ├── grpc.go │ │ ├── grpc_test.go │ │ ├── param_loader.go │ │ └── sdid_parser.go │ └── rpc │ │ ├── protocol │ │ ├── protocol.go │ │ └── protocol_impl │ │ │ ├── constant.go │ │ │ ├── iocgo.go │ │ │ ├── param.go │ │ │ ├── util.go │ │ │ └── zz_generated.ioc.go │ │ ├── proxy │ │ ├── metadata.go │ │ └── proxy_invoker.go │ │ ├── rpc_client │ │ ├── param.go │ │ ├── param_loader.go │ │ ├── param_loader_config.go │ │ ├── param_loader_tag_point_to_config.go │ │ ├── proxy_impl.go │ │ ├── proxy_invoker.go │ │ ├── rpc_client.go │ │ ├── rpc_client_test.go │ │ └── sdid_parser.go │ │ └── rpc_service │ │ ├── param.go │ │ ├── param_test.go │ │ ├── rpc_service.go │ │ └── rpc_service_test.go ├── config │ ├── config_float64_describer.go │ ├── config_int64_describer.go │ ├── config_int_describer.go │ ├── config_map_describer.go │ ├── config_slice_describer.go │ ├── config_string_describer.go │ └── zz_generated.ioc.go ├── config_center │ └── nacos │ │ ├── nacos.go │ │ ├── param.go │ │ └── zz_generated.ioc.go ├── db │ └── gorm │ │ ├── README.md │ │ ├── gorm_db.go │ │ ├── param.go │ │ ├── param_test.go │ │ ├── test │ │ └── ioc_golang.yaml │ │ └── zz_generated.ioc.go ├── imports │ ├── boot │ │ ├── imports.go │ │ └── imports_default.go │ └── cli │ │ └── imports.go ├── pubsub │ └── rocketmq │ │ ├── param.go │ │ ├── rocketmq.go │ │ └── zz_generated.ioc.go ├── registry │ └── nacos │ │ ├── nacos.go │ │ ├── param.go │ │ └── zz_generated.ioc.go └── state │ └── redis │ ├── README.md │ ├── param.go │ ├── redis.go │ └── zz_generated.ioc.go ├── go.mod ├── go.sum ├── iocli ├── Makefile ├── README.md ├── README_EN.md ├── gen │ ├── cmd.go │ ├── generator │ │ ├── context.go │ │ ├── generator.go │ │ ├── plugin │ │ │ ├── code_writer.go │ │ │ ├── common │ │ │ │ ├── file_parser.go │ │ │ │ ├── file_parser_test.go │ │ │ │ ├── interface_generator.go │ │ │ │ ├── model.go │ │ │ │ ├── proxy_struct_generator.go │ │ │ │ └── util.go │ │ │ ├── plugin.go │ │ │ └── sorter.go │ │ ├── register.go │ │ ├── rpc_client.go │ │ ├── rpc_client_test.go │ │ ├── writer.go │ │ └── zz_generated.ioc.go │ └── marker │ │ ├── getter.go │ │ └── impls │ │ ├── internal.go │ │ └── zz_generated.ioc.go ├── init │ ├── cmd.go │ ├── init-packr.go │ ├── init.go │ ├── init_test.go │ ├── option.go │ ├── packrd │ │ └── packed-packr.go │ └── templates │ │ ├── .gitignore.tmpl │ │ ├── README.md.tmpl │ │ ├── cmd │ │ └── app │ │ │ ├── app.go.tmpl │ │ │ ├── service.go.tmpl │ │ │ └── zz_generated.ioc.go.tmpl │ │ ├── configs │ │ └── ioc_golang.yaml.tmpl │ │ ├── docs │ │ └── .Keep.tmpl │ │ ├── go.mod.tmpl │ │ ├── internal │ │ └── .Keep.tmpl │ │ ├── main.go.tmpl │ │ └── pkg │ │ └── .Keep.tmpl ├── main.go └── root │ ├── root.go │ └── root_test.go ├── logger ├── logger.go └── logger_test.go ├── logo.go └── test ├── docker_compose └── dockercompose.go ├── iocli_command └── iocli_command.go └── stress ├── aop ├── normal_app.go ├── proxy_test.go ├── recursive_app.go ├── service.go └── zz_generated.ioc.go ├── autowire ├── normal_app.go ├── normal_test.go ├── service.go ├── singleton_app.go ├── singleton_test.go └── zz_generated.ioc.go └── panic ├── panic_after.go ├── panic_after_test.go └── zz_generated.ioc.go /.github/workflows/github-actions.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [master, develop] 6 | pull_request: 7 | branches: "*" 8 | 9 | jobs: 10 | build: 11 | name: ${{ matrix.os }} - Go ${{ matrix.go_version }} 12 | runs-on: ${{ matrix.os }} 13 | strategy: 14 | # If you want to matrix build , you can append the following list. 15 | matrix: 16 | go_version: 17 | - 1.18 18 | os: 19 | - ubuntu-latest 20 | steps: 21 | - name: Set up Go ${{ matrix.go_version }} 22 | uses: actions/setup-go@v2 23 | with: 24 | go-version: ${{ matrix.go_version }} 25 | id: go 26 | 27 | - name: Check out code into the Go module directory 28 | uses: actions/checkout@v3 29 | 30 | - name: Get dependencies 31 | run: | 32 | make tidy-all 33 | - name: gofmt 34 | run: | 35 | go fmt ./... 36 | - name: build iocli 37 | run : | 38 | cd ./iocli && go build -o /usr/local/bin . 39 | 40 | - name: iocli gen 41 | run : | 42 | /usr/local/bin/iocli gen 43 | - name: Unit Test 44 | run: | 45 | make test-all -------------------------------------------------------------------------------- /.github/workflows/golangci-lint.yml: -------------------------------------------------------------------------------- 1 | name: golangci-lint 2 | on: 3 | push: 4 | branches: [master, develop] 5 | pull_request: 6 | branches: "*" 7 | 8 | jobs: 9 | golangci: 10 | name: lint 11 | runs-on: ubuntu-latest 12 | strategy: 13 | matrix: 14 | golang: 15 | - 1.18 16 | steps: 17 | - uses: actions/setup-go@v2 18 | with: 19 | go-version: 1.18 20 | - uses: actions/checkout@v3 21 | - name: golangci-lint 22 | uses: golangci/golangci-lint-action@v3.1.0 23 | with: 24 | version: v1.46.1 25 | args: --timeout=10m 26 | skip-go-installation: true 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | .idea 3 | *.so 4 | iocli/.release 5 | .release -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | Alibaba has adopted a Code of Conduct that we expect project participants to adhere to. 4 | 5 | Please refer to [Alibaba Open Source Code of Conduct](https://github.com/AlibabaDR/community/blob/master/CODE_OF_CONDUCT.md) ([中文版](https://github.com/AlibabaDR/community/blob/master/CODE_OF_CONDUCT_zh.md)). -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | IOC-golang 2 | Copyright (c) 2022, Alibaba Group. 3 | Licensed under the Apache License, Version 2.0 -------------------------------------------------------------------------------- /aop/common/config.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package common 17 | 18 | type Config struct { 19 | AppName string `yaml:"app-name"` 20 | Disable bool `yaml:"disable"` 21 | DebugServer DebugServerConfig `yaml:"debug-server"` 22 | } 23 | 24 | type DebugServerConfig struct { 25 | Port string `yaml:"port"` 26 | Disable bool `yaml:"disable"` 27 | } 28 | 29 | type ConfigLoader func(config *Config) 30 | 31 | const IOCGolangAOPConfigPrefix = "ioc-golang.aop" 32 | -------------------------------------------------------------------------------- /aop/common/listener.go: -------------------------------------------------------------------------------- 1 | package common 2 | 3 | import ( 4 | "net" 5 | "strconv" 6 | 7 | "github.com/alibaba/ioc-golang/logger" 8 | ) 9 | 10 | func GetTCPListener(port string) (net.Listener, error) { 11 | lst, err := net.Listen("tcp", ":"+port) 12 | for err != nil { 13 | portInt, iToAError := strconv.Atoi(port) 14 | if iToAError != nil { 15 | logger.Blue("[Debug] Debug server listening with invalid port :%s, error = %s", port, iToAError) 16 | return nil, iToAError 17 | } 18 | logger.Blue("[Debug] Debug server listening port :%s failed with error = %s, try to bind %d", port, err, portInt+1) 19 | port = strconv.Itoa(portInt + 1) 20 | lst, err = net.Listen("tcp", ":"+port) 21 | } 22 | return lst, nil 23 | } 24 | -------------------------------------------------------------------------------- /aop/common/metadata.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package common 17 | 18 | import ( 19 | "sync" 20 | ) 21 | 22 | type StructMetadata struct { 23 | MethodMetadata map[string]*MethodMetadata 24 | } 25 | 26 | type MethodMetadata struct { 27 | Lock sync.Mutex 28 | } 29 | 30 | type AllInterfaceMetadata map[string]*StructMetadata 31 | -------------------------------------------------------------------------------- /aop/common/runtime.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package common 17 | 18 | import ( 19 | "runtime" 20 | "strings" 21 | ) 22 | 23 | const ( 24 | ProxyMethodPrefix = "github.com/alibaba/ioc-golang/aop." 25 | ) 26 | 27 | func CurrentCallingMethodName(skip int) string { 28 | pc := make([]uintptr, 1) 29 | runtime.Callers(skip, pc) 30 | return runtime.FuncForPC(pc[0]).Name() 31 | } 32 | 33 | func IsTraceEntrance(entranceMethodFullName string) bool { 34 | pc := make([]uintptr, 500) 35 | n := runtime.Callers(0, pc) 36 | foundEntrance := false 37 | level := int64(0) 38 | 39 | for i := n - 1; i >= 0; i-- { 40 | fName := runtime.FuncForPC(pc[i]).Name() 41 | if foundEntrance { 42 | if strings.HasPrefix(fName, ProxyMethodPrefix) { 43 | level++ 44 | } 45 | if level == 3 { 46 | return false 47 | } 48 | continue 49 | } 50 | if fName == entranceMethodFullName { 51 | foundEntrance = true 52 | } 53 | } 54 | 55 | return level-2 == 0 56 | } 57 | -------------------------------------------------------------------------------- /aop/common/test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package common 17 | 18 | import ( 19 | "context" 20 | ) 21 | 22 | type User struct { 23 | Name string 24 | } 25 | 26 | type RequestParam struct { 27 | User *User 28 | } 29 | 30 | type Response struct { 31 | Name string 32 | } 33 | 34 | type ServiceFoo struct { 35 | } 36 | 37 | func (s *ServiceFoo) Invoke(ctx context.Context, param *RequestParam) (*Response, error) { 38 | return &Response{ 39 | Name: param.User.Name, 40 | }, nil 41 | } 42 | -------------------------------------------------------------------------------- /aop/metadata.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package aop 17 | 18 | import "github.com/alibaba/ioc-golang/autowire" 19 | 20 | const MetadataKey = "aop" 21 | 22 | type Metadata map[string]interface{} 23 | 24 | func ParseAOPMetadataFromSDMetadata(metadata autowire.Metadata) Metadata { 25 | if metadata == nil { 26 | return nil 27 | } 28 | if aopMetadataVal, ok := metadata[MetadataKey]; ok { 29 | if aopMetadata, ok2 := aopMetadataVal.(map[string]interface{}); ok2 { 30 | return aopMetadata 31 | } 32 | } 33 | return nil 34 | } 35 | -------------------------------------------------------------------------------- /aop/server.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package aop 17 | 18 | import ( 19 | "math" 20 | "net" 21 | 22 | "google.golang.org/grpc" 23 | 24 | "github.com/alibaba/ioc-golang/aop/common" 25 | "github.com/alibaba/ioc-golang/logger" 26 | ) 27 | 28 | // startDebugServer should be called only when aop is enabled 29 | func startDebugServer(aopConfig *common.Config) error { 30 | if !enabled { 31 | return nil 32 | } 33 | 34 | // new grpc server 35 | grpcServer := grpc.NewServer(grpc.MaxRecvMsgSize(math.MaxInt32)) 36 | 37 | // register sop service 38 | for _, register := range grpcServiceRegisters { 39 | register(grpcServer) 40 | } 41 | 42 | lst, err := common.GetTCPListener(aopConfig.DebugServer.Port) 43 | if err != nil { 44 | return err 45 | } 46 | 47 | // start server 48 | go func() { 49 | logger.Blue("[Debug] Debug server listening at :%d", lst.Addr().(*net.TCPAddr).Port) 50 | if err := grpcServer.Serve(lst); err != nil { 51 | logger.Red("[Debug] Debug server run with error = ", err) 52 | return 53 | } 54 | }() 55 | return nil 56 | } 57 | -------------------------------------------------------------------------------- /autowire/alias.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package autowire 17 | 18 | import ( 19 | "fmt" 20 | ) 21 | 22 | const ( 23 | AliasKey = "alias" 24 | ) 25 | 26 | // sdIDAliasMap a map of SDID alias-mapping named as alias container. 27 | var sdIDAliasMap = make(map[string]string) 28 | 29 | func registerAlias(alias, value string) { 30 | if _, ok := sdIDAliasMap[alias]; ok { 31 | panic(fmt.Sprintf("[Autowire Alias] Duplicate alias:[%s]", alias)) 32 | } 33 | 34 | sdIDAliasMap[alias] = value 35 | } 36 | 37 | func GetSDIDByAliasIfNecessary(key string) string { 38 | if mappingSDID, ok := GetSDIDByAlias(key); ok { 39 | return mappingSDID 40 | } 41 | 42 | return key 43 | } 44 | 45 | func GetSDIDByAlias(alias string) (string, bool) { 46 | sdid, ok := sdIDAliasMap[alias] 47 | return sdid, ok 48 | } 49 | -------------------------------------------------------------------------------- /autowire/proxy.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package autowire 17 | 18 | // proxy function 19 | 20 | var pf func(interface{}) interface{} 21 | 22 | func RegisterProxyFunction(f func(interface{}) interface{}) { 23 | pf = f 24 | } 25 | 26 | func GetProxyFunction() func(interface{}) interface{} { 27 | if pf == nil { 28 | return func(i interface{}) interface{} { 29 | return i 30 | } 31 | } 32 | return pf 33 | } 34 | 35 | // proxy impl function 36 | 37 | var pif func(interface{}, interface{}, string) error 38 | 39 | func RegisterProxyImplFunction(f func(interface{}, interface{}, string) error) { 40 | pif = f 41 | } 42 | 43 | func GetProxyImplFunction() func(interface{}, interface{}, string) error { 44 | if pif == nil { 45 | return func(interface{}, interface{}, string) error { 46 | return nil 47 | } 48 | } 49 | return pif 50 | } 51 | -------------------------------------------------------------------------------- /autowire/struct_descriptor.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package autowire 17 | 18 | var structDescriptorsMap = make(map[string]*StructDescriptor) 19 | 20 | func RegisterStructDescriptor(sd *StructDescriptor) { 21 | // 1. register struct descriptor 22 | structDescriptorsMap[sd.ID()] = sd 23 | 24 | // 2. register alias if necessary 25 | if sd.Alias != "" { 26 | registerAlias(sd.Alias, sd.ID()) 27 | } 28 | 29 | // 3. register struct implements info 30 | registerImplements(sd) 31 | } 32 | 33 | func GetStructDescriptor(sdid string) *StructDescriptor { 34 | return structDescriptorsMap[sdid] 35 | } 36 | 37 | func GetStructDescriptorsMap() map[string]*StructDescriptor { 38 | return structDescriptorsMap 39 | } 40 | -------------------------------------------------------------------------------- /boot.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package ioc 17 | 18 | import ( 19 | "github.com/alibaba/ioc-golang/aop" 20 | "github.com/alibaba/ioc-golang/autowire" 21 | "github.com/alibaba/ioc-golang/config" 22 | "github.com/alibaba/ioc-golang/logger" 23 | 24 | _ "github.com/alibaba/ioc-golang/extension/imports/boot" 25 | ) 26 | 27 | const Version = "1.0.3" 28 | 29 | func Load(opts ...config.Option) error { 30 | printLogo() 31 | logger.Cyan("Welcome to use ioc-golang %s!", Version) 32 | 33 | // 1. load config 34 | logger.Blue("[Boot] Start to load ioc-golang config") 35 | if err := config.Load(opts...); err != nil { 36 | return err 37 | } 38 | 39 | // 2. load debug 40 | logger.Blue("[Boot] Start to load debug") 41 | if err := aop.Load(); err != nil { 42 | return err 43 | } 44 | 45 | // 3. load autowire 46 | logger.Blue("[Boot] Start to load autowire") 47 | return autowire.Load() 48 | } 49 | -------------------------------------------------------------------------------- /config/regepx.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "regexp" 5 | ) 6 | 7 | func isEnv(envValue string) bool { 8 | // ${ Xxx_Yyy_Zzz } 9 | ok, err := regexp.Match("^\\$\\{[A-Z_]+}$", []byte(envValue)) 10 | if err != nil || !ok { 11 | return false 12 | } 13 | 14 | return true 15 | } 16 | -------------------------------------------------------------------------------- /config/regepx_test.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_isEnv(t *testing.T) { 10 | type args struct { 11 | envValue string 12 | } 13 | tests := []struct { 14 | name string 15 | args args 16 | want bool 17 | }{ 18 | { 19 | name: "test isEnv-true", 20 | args: args{ 21 | envValue: "${REDIS_ADDRESS_EXPAND}", 22 | }, 23 | want: true, 24 | }, 25 | { 26 | name: "test isEnv-false-1", 27 | args: args{ 28 | envValue: "REDIS_ADDRESS_EXPAND", 29 | }, 30 | want: false, 31 | }, 32 | { 33 | name: "test isEnv-false-2", 34 | args: args{ 35 | envValue: "${REDIS_ADDRESS_EXPAND", 36 | }, 37 | want: false, 38 | }, 39 | { 40 | name: "test isEnv-false-3", 41 | args: args{ 42 | envValue: "REDIS_ADDRESS_EXPAND}", 43 | }, 44 | want: false, 45 | }, 46 | } 47 | for _, tt := range tests { 48 | t.Run(tt.name, func(t *testing.T) { 49 | assert.Equalf(t, tt.want, isEnv(tt.args.envValue), "isEnv(%v)", tt.args.envValue) 50 | }) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /config/test/ioc_golang-bad-config.yaml: -------------------------------------------------------------------------------- 1 | autowire 2 | normal: 3 | github.com/alibaba/ioc-golang/extension/state/redis.Redis: 4 | db1-redis: 5 | param: 6 | address: localhost:16379 7 | db: 1 8 | db2-redis: 9 | param: 10 | address: localhost:26379 11 | db: 2 12 | param: 13 | address: localhost:6379 14 | db: 0 -------------------------------------------------------------------------------- /config/test/ioc_golang.yaml: -------------------------------------------------------------------------------- 1 | autowire: 2 | config: 3 | strValue: strVal 4 | intValue: 123 5 | mapValue: 6 | mapKey1: mapValue1 7 | mapKey2: mapValue2 8 | mapKey3: mapValue3 9 | sliceValue: 10 | - sliceStr1 11 | - sliceStr2 12 | - sliceStr3 13 | normal: 14 | github.com/alibaba/ioc-golang/extension/state/redis.Redis: 15 | db1-redis: 16 | param: 17 | address: localhost:16379 18 | db: 1 19 | db2-redis: 20 | param: 21 | address: localhost:26379 22 | db: 2 23 | param: 24 | address: localhost:6379 25 | db: 0 26 | expand: 27 | address: ${REDIS_ADDRESS_EXPAND} 28 | db: 15 29 | nested: 30 | address: ${autowire.normal..expand.address} 31 | db: ${autowire.normal..expand.db} -------------------------------------------------------------------------------- /config/test/ioc_golang_dev.yaml: -------------------------------------------------------------------------------- 1 | profilesActive: 2 | shared: 3 | strValue: world 4 | intValue: 123 5 | boolValue: true 6 | mapValue: 7 | mapKey1: mapValue1 8 | mapKey2: mapValue2 9 | mapKey3: mapValue3 10 | sliceValue: 11 | - sliceStr1 12 | - sliceStr2 13 | - sliceStr3 -------------------------------------------------------------------------------- /example/aop/README.md: -------------------------------------------------------------------------------- 1 | # AOP 接口代理能力的例子 2 | 3 | - [call](./call) 4 | 5 | 通过 iocli 工具实时调试接口例子 6 | 7 | - [logs](./log) 8 | 9 | 通过 iocli 工具实时拉取调用链路/接口/方法日志的例子 10 | 11 | - [dynamic_plugin](./dynamic_plugin) 12 | 13 | 基于 Go 插件的接口动态代理例子 14 | 15 | - [observability](./observability) 16 | 17 | 可视化能力例子,包括接口展示、方法rt监控、全链路追踪 18 | 19 | - [transaction](./transaction) 20 | 21 | 事务能力例子,包括单体应用、分布式事务 22 | 23 | -------------------------------------------------------------------------------- /example/aop/call/dto/user.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package dto 17 | 18 | type User struct { 19 | Id int 20 | Name string 21 | Age int 22 | Mark string 23 | } 24 | -------------------------------------------------------------------------------- /example/aop/dynamic_plugin/complex/config/config.yaml: -------------------------------------------------------------------------------- 1 | autowire: 2 | normal: 3 | github.com/alibaba/ioc-golang/extension/state/redis.Redis: 4 | db1-redis: 5 | param: 6 | address: localhost:6379 7 | db: 1 -------------------------------------------------------------------------------- /example/aop/dynamic_plugin/complex/docker-compose/docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: '3.1' 2 | services: 3 | demo-redis: 4 | image: redis:latest 5 | container_name: demo-redis 6 | ports: 7 | - "6379:6379" -------------------------------------------------------------------------------- /example/aop/dynamic_plugin/complex/service1/service.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package service1 17 | 18 | // +ioc:autowire=true 19 | // +ioc:autowire:type=singleton 20 | // +ioc:autowire:type=normal 21 | // +ioc:autowire:constructFunc=constructFunc 22 | 23 | type Service1 struct { 24 | name string 25 | } 26 | 27 | func constructFunc(s *Service1) (*Service1, error) { 28 | s.name = "default" 29 | return s, nil 30 | } 31 | 32 | func (s *Service1) SetName(name string) { 33 | s.name = name 34 | } 35 | 36 | func (s *Service1) GetName() string { 37 | return s.name 38 | } 39 | -------------------------------------------------------------------------------- /example/aop/dynamic_plugin/simple/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | "time" 21 | 22 | "github.com/alibaba/ioc-golang" 23 | ) 24 | 25 | // +ioc:autowire=true 26 | // +ioc:autowire:type=singleton 27 | 28 | type App struct { 29 | Service1OwnInterface ServiceImpl1IOCInterface `singleton:""` 30 | } 31 | 32 | func (a *App) Run() { 33 | for { 34 | time.Sleep(time.Second * 3) 35 | fmt.Println(a.Service1OwnInterface.GetHelloString("laurence")) 36 | } 37 | } 38 | 39 | // +ioc:autowire=true 40 | // +ioc:autowire:type=singleton 41 | 42 | type ServiceImpl1 struct { 43 | } 44 | 45 | func (s *ServiceImpl1) GetHelloString(name string) string { 46 | return fmt.Sprintf("This is ServiceImpl1, hello %s", name) 47 | } 48 | 49 | func main() { 50 | // start 51 | if err := ioc.Load(); err != nil { 52 | panic(err) 53 | } 54 | 55 | app, err := GetAppSingleton() 56 | if err != nil { 57 | panic(err) 58 | } 59 | app.Run() 60 | } 61 | -------------------------------------------------------------------------------- /example/aop/dynamic_plugin/simple/plugin_1/service_plugin.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import "fmt" 19 | 20 | type ServiceImpl1 struct { 21 | } 22 | 23 | func (s *ServiceImpl1) GetHelloString(name string) string { 24 | return fmt.Sprintf("This is Plugin, hello %s", name) 25 | } 26 | 27 | // nolint 28 | var Service1Plugin = ServiceImpl1{} 29 | -------------------------------------------------------------------------------- /example/aop/log/README.md: -------------------------------------------------------------------------------- 1 | # 日志 AOP 能力 2 | 3 | ioc-golang 提供了基于 [logrus](https://github.com/sirupsen/logrus) 日志库的扩展。 4 | 5 | ## 框架自动填充接口调用信息日志 6 | 7 | ## 框架为用户基于default logger 打印的日志增加接口,方法信息 field 8 | 9 | ## 基于 iocli 的链路日志拉取/展示能力 -------------------------------------------------------------------------------- /example/aop/log/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "github.com/alibaba/ioc-golang" 20 | "github.com/alibaba/ioc-golang/example/aop/log/app" 21 | ) 22 | 23 | func main() { 24 | // start 25 | if err := ioc.Load(); err != nil { 26 | panic(err) 27 | } 28 | 29 | application, err := app.GetAppSingleton() 30 | if err != nil { 31 | panic(err) 32 | } 33 | application.Run() 34 | } 35 | -------------------------------------------------------------------------------- /example/aop/log/test_file/test_global_logger_readonly/main_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package test_default 17 | 18 | import ( 19 | "testing" 20 | 21 | "github.com/sirupsen/logrus" 22 | "github.com/stretchr/testify/assert" 23 | 24 | "github.com/alibaba/ioc-golang" 25 | "github.com/alibaba/ioc-golang/aop/common" 26 | "github.com/alibaba/ioc-golang/config" 27 | "github.com/alibaba/ioc-golang/example/aop/log/app" 28 | aopLog "github.com/alibaba/ioc-golang/extension/aop/log" 29 | ) 30 | 31 | func TestGlobalLoggerReadonlyConfiguration(t *testing.T) { 32 | assert.Nil(t, ioc.Load( 33 | config.AddProperty(common.IOCGolangAOPConfigPrefix+"."+aopLog.Name+".global-logger-read-only", true), 34 | config.AddProperty(common.IOCGolangAOPConfigPrefix+"."+aopLog.Name+".level", "debug"), 35 | )) 36 | _, err := app.GetAppIOCInterfaceSingleton() 37 | assert.Nil(t, err) 38 | assert.Equal(t, logrus.InfoLevel, logrus.GetLevel()) 39 | } 40 | -------------------------------------------------------------------------------- /example/aop/transaction/distributed/server/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "github.com/alibaba/ioc-golang" 20 | _ "github.com/alibaba/ioc-golang/example/aop/transaction/distributed/server/pkg/service" 21 | ) 22 | 23 | func main() { 24 | // start 25 | if err := ioc.Load(); err != nil { 26 | panic(err) 27 | } 28 | select {} 29 | } 30 | -------------------------------------------------------------------------------- /example/autowire/autowire_active_profile_implements/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | "time" 21 | 22 | "github.com/alibaba/ioc-golang" 23 | "github.com/alibaba/ioc-golang/example/autowire/autowire_active_profile_implements/service" 24 | _ "github.com/alibaba/ioc-golang/example/autowire/autowire_active_profile_implements/service/impl" 25 | ) 26 | 27 | // +ioc:autowire=true 28 | // +ioc:autowire:type=singleton 29 | // +ioc:autowire:type=normal 30 | 31 | type App struct { 32 | ServiceImpl service.Service `singleton:""` // inject implement ptr with active profile like 'dev' 33 | } 34 | 35 | func (a *App) Run() { 36 | for { 37 | time.Sleep(time.Second * 3) 38 | fmt.Println(a.ServiceImpl.GetHelloString("laurence")) 39 | } 40 | } 41 | 42 | func main() { 43 | // start 44 | if err := ioc.Load(); err != nil { 45 | panic(err) 46 | } 47 | 48 | app, err := GetAppSingleton() 49 | if err != nil { 50 | panic(err) 51 | } 52 | app.Run() 53 | } 54 | -------------------------------------------------------------------------------- /example/autowire/autowire_active_profile_implements/service/impl/default.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package impl 17 | 18 | import "fmt" 19 | 20 | // +ioc:autowire=true 21 | // +ioc:autowire:type=singleton 22 | // +ioc:autowire:implements=github.com/alibaba/ioc-golang/example/autowire/autowire_active_profile_implements/service.Service 23 | 24 | type serviceDefaultImpl struct { 25 | } 26 | 27 | func (s *serviceDefaultImpl) GetHelloString(name string) string { 28 | return fmt.Sprintf("This is serviceDefaultImpl, hello %s", name) 29 | } 30 | -------------------------------------------------------------------------------- /example/autowire/autowire_active_profile_implements/service/impl/dev.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package impl 17 | 18 | import "fmt" 19 | 20 | // +ioc:autowire=true 21 | // +ioc:autowire:type=singleton 22 | // +ioc:autowire:activeProfile=dev 23 | // +ioc:autowire:implements=github.com/alibaba/ioc-golang/example/autowire/autowire_active_profile_implements/service.Service 24 | 25 | type serviceDevImpl struct { 26 | } 27 | 28 | func (s *serviceDevImpl) GetHelloString(name string) string { 29 | return fmt.Sprintf("This is serviceDevImpl, hello %s", name) 30 | } 31 | -------------------------------------------------------------------------------- /example/autowire/autowire_active_profile_implements/service/impl/pro.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package impl 17 | 18 | import "fmt" 19 | 20 | // +ioc:autowire=true 21 | // +ioc:autowire:type=singleton 22 | // +ioc:autowire:activeProfile=pro 23 | // +ioc:autowire:implements=github.com/alibaba/ioc-golang/example/autowire/autowire_active_profile_implements/service.Service 24 | 25 | type serviceProImpl struct { 26 | } 27 | 28 | func (s *serviceProImpl) GetHelloString(name string) string { 29 | return fmt.Sprintf("This is serviceProImpl, hello %s", name) 30 | } 31 | -------------------------------------------------------------------------------- /example/autowire/autowire_active_profile_implements/service/service.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package service 17 | 18 | type Service interface { 19 | GetHelloString(string) string 20 | } 21 | -------------------------------------------------------------------------------- /example/autowire/autowire_allimpls/service/impl/impl1.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package impl 17 | 18 | import "fmt" 19 | 20 | // +ioc:autowire=true 21 | // +ioc:autowire:type=allimpls 22 | // +ioc:autowire:implements=github.com/alibaba/ioc-golang/example/autowire/autowire_allimpls/service.Service 23 | 24 | type serviceImpl1 struct { 25 | } 26 | 27 | func (s *serviceImpl1) GetHelloString( 28 | name string) string { 29 | return fmt.Sprintf("This is ServiceImpl2, hello %s", name) 30 | } 31 | -------------------------------------------------------------------------------- /example/autowire/autowire_allimpls/service/impl/impl2.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package impl 17 | 18 | import "fmt" 19 | 20 | // +ioc:autowire=true 21 | // +ioc:autowire:type=allimpls 22 | // +ioc:autowire:implements=github.com/alibaba/ioc-golang/example/autowire/autowire_allimpls/service.Service 23 | 24 | type serviceImpl2 struct { 25 | } 26 | 27 | func (s *serviceImpl2) GetHelloString( 28 | name string, 29 | ) string { 30 | return fmt.Sprintf("This is ServiceImpl1, hello %s", name) 31 | } 32 | -------------------------------------------------------------------------------- /example/autowire/autowire_allimpls/service/impl3.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package service 17 | 18 | import "fmt" 19 | 20 | // +ioc:autowire=true 21 | // +ioc:autowire:type=allimpls 22 | // +ioc:autowire:implements=github.com/alibaba/ioc-golang/example/autowire/autowire_allimpls/service.Service 23 | 24 | type serviceImpl3 struct { 25 | } 26 | 27 | func (s *serviceImpl3) GetHelloString(name string) string { 28 | return fmt.Sprintf("This is ServiceImpl3, hello %s", name) 29 | } 30 | -------------------------------------------------------------------------------- /example/autowire/autowire_allimpls/service/impl4.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package service 17 | 18 | import "fmt" 19 | 20 | // +ioc:autowire=true 21 | // +ioc:autowire:type=allimpls 22 | // +ioc:autowire:implements=Service 23 | 24 | type serviceImpl4 struct { 25 | } 26 | 27 | func (s *serviceImpl4) GetHelloString(name string) string { 28 | return fmt.Sprintf("This is ServiceImpl4, hello %s", name) 29 | } 30 | -------------------------------------------------------------------------------- /example/autowire/autowire_allimpls/service/service.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package service 17 | 18 | type Service interface { 19 | GetHelloString(string) string 20 | } 21 | -------------------------------------------------------------------------------- /example/autowire/autowire_config/cmd/main_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | "testing" 21 | 22 | "github.com/alibaba/ioc-golang/config" 23 | 24 | "github.com/stretchr/testify/assert" 25 | 26 | "github.com/alibaba/ioc-golang" 27 | ) 28 | 29 | func TestConfig(t *testing.T) { 30 | if err := ioc.Load( 31 | config.WithSearchPath("../conf")); err != nil { 32 | panic(err) 33 | } 34 | app, err := GetAppSingleton() 35 | if err != nil { 36 | panic(err) 37 | } 38 | assert.Equal(t, "stringValue", app.DemoConfigString.Value()) 39 | assert.Equal(t, 123, app.DemoConfigInt.Value()) 40 | assert.Equal(t, "map[key1:value1 key2:value2 key3:value3 obj:map[objkey1:objvalue1 objkey2:objvalue2 objkeyslice:objslicevalue]]", fmt.Sprint(app.DemoConfigMap.Value())) 41 | assert.Equal(t, "[sliceValue1 sliceValue2 sliceValue3 sliceValue4]", fmt.Sprint(app.DemoConfigSlice.Value())) 42 | } 43 | -------------------------------------------------------------------------------- /example/autowire/autowire_config/conf/config.yaml: -------------------------------------------------------------------------------- 1 | autowire: 2 | config: 3 | demo-config: 4 | int-value: 123 5 | int64-value: 130117537261158665 6 | float64-value: 0.001 7 | string-value: stringValue 8 | map-value: 9 | key1: value1 10 | key2: value2 11 | key3: value3 12 | obj: 13 | objkey1: objvalue1 14 | objkey2: objvalue2 15 | objkeyslice: objslicevalue 16 | slice-value: 17 | - sliceValue1 18 | - sliceValue2 19 | - sliceValue3 20 | - sliceValue4 -------------------------------------------------------------------------------- /example/autowire/autowire_rpc/client/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | "time" 21 | 22 | "github.com/alibaba/ioc-golang" 23 | "github.com/alibaba/ioc-golang/example/autowire/autowire_rpc/server/pkg/service/api" 24 | ) 25 | 26 | // +ioc:autowire=true 27 | // +ioc:autowire:type=singleton 28 | 29 | type App struct { 30 | ServiceStruct api.ServiceStructIOCRPCClient `rpc-client:",address=127.0.0.1:2022"` 31 | } 32 | 33 | func (a *App) Run() { 34 | for { 35 | time.Sleep(time.Second * 3) 36 | usr, err := a.ServiceStruct.GetUser("laurence", 23) 37 | if err != nil { 38 | fmt.Println(err) 39 | } 40 | fmt.Printf("get user = %+v\n", usr) 41 | } 42 | } 43 | 44 | func main() { 45 | // start 46 | if err := ioc.Load(); err != nil { 47 | panic(err) 48 | } 49 | 50 | // 'App' is alias name 51 | // We can get instance by ths id 52 | app, err := GetAppSingleton() 53 | if err != nil { 54 | panic(err) 55 | } 56 | app.Run() 57 | } 58 | -------------------------------------------------------------------------------- /example/autowire/autowire_rpc/client/main_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "testing" 20 | "time" 21 | 22 | "github.com/stretchr/testify/assert" 23 | 24 | "github.com/alibaba/ioc-golang" 25 | 26 | _ "github.com/alibaba/ioc-golang/example/autowire/autowire_rpc/server/pkg/service" 27 | ) 28 | 29 | func (a *App) TestRun(t *testing.T) { 30 | usr, err := a.ServiceStruct.GetUser("laurence", 23) 31 | assert.Nil(t, err) 32 | assert.NotNil(t, usr) 33 | assert.Equal(t, 1, usr.Id) 34 | assert.Equal(t, "laurence", usr.Name) 35 | assert.Equal(t, 23, usr.Age) 36 | } 37 | 38 | func TestRPCClient(t *testing.T) { 39 | if err := ioc.Load(); err != nil { 40 | panic(err) 41 | } 42 | app, err := GetAppSingleton() 43 | if err != nil { 44 | panic(err) 45 | } 46 | time.Sleep(time.Second) 47 | app.TestRun(t) 48 | } 49 | -------------------------------------------------------------------------------- /example/autowire/autowire_rpc/client/test/dto/custom.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package dto 17 | 18 | type CustomStruct struct { 19 | User // combination 20 | CustomStructId int64 21 | IdPtr *int64 22 | CustomStructName string 23 | NamePtr *string 24 | CustomStringMap map[string]string 25 | CustomIntMap map[string]int 26 | CustomSubStructPtrMap map[string]*User 27 | CustomSubStructMap map[string]User 28 | StringSlice []string 29 | SubStructSlice []User 30 | SubStructPtrSlice []*User 31 | SubStruct User 32 | SubStructPtr *User 33 | } 34 | 35 | func (c *CustomStruct) GetUser() User { 36 | return c.SubStruct 37 | } 38 | -------------------------------------------------------------------------------- /example/autowire/autowire_rpc/client/test/dto/user.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package dto 17 | 18 | type User struct { 19 | Id int 20 | Name string 21 | Age int 22 | } 23 | -------------------------------------------------------------------------------- /example/autowire/autowire_rpc/client/test/service/api/zz_generated.ioc_rpc_client_simpleservice.go: -------------------------------------------------------------------------------- 1 | // Code generated by iocli, run 'iocli gen' to re-generate 2 | 3 | package api 4 | 5 | import ( 6 | "github.com/alibaba/ioc-golang/autowire" 7 | "github.com/alibaba/ioc-golang/autowire/normal" 8 | "github.com/alibaba/ioc-golang/example/autowire/autowire_rpc/client/test/dto" 9 | "github.com/alibaba/ioc-golang/extension/autowire/rpc/rpc_client" 10 | ) 11 | 12 | func init() { 13 | rpc_client.RegisterStructDescriptor(&autowire.StructDescriptor{ 14 | Factory: func() interface{} { 15 | return &simpleServiceIOCRPCClient{} 16 | }, 17 | Metadata: map[string]interface{}{ 18 | "aop": map[string]interface{}{}, 19 | "autowire": map[string]interface{}{}, 20 | }, 21 | }) 22 | normal.RegisterStructDescriptor(&autowire.StructDescriptor{ 23 | Factory: func() interface{} { 24 | return &simpleServiceIOCRPCClient_{} 25 | }, 26 | }) 27 | } 28 | 29 | type simpleServiceIOCRPCClient_ struct { 30 | GetUser_ func(name string, age int) (*dto.User, error) 31 | } 32 | 33 | func (s *simpleServiceIOCRPCClient_) GetUser(name string, age int) (*dto.User, error) { 34 | return s.GetUser_(name, age) 35 | } 36 | 37 | type SimpleServiceIOCRPCClient interface { 38 | GetUser(name string, age int) (*dto.User, error) 39 | } 40 | 41 | type simpleServiceIOCRPCClient struct { 42 | GetUser func(name string, age int) (*dto.User, error) 43 | } 44 | -------------------------------------------------------------------------------- /example/autowire/autowire_rpc/client/test/service/simple.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package service 17 | 18 | import ( 19 | "github.com/alibaba/ioc-golang/example/autowire/autowire_rpc/client/test/dto" 20 | ) 21 | 22 | // +ioc:autowire=true 23 | // +ioc:autowire:type=rpc 24 | 25 | type SimpleService struct { 26 | } 27 | 28 | func (s *SimpleService) GetUser(name string, age int) (*dto.User, error) { 29 | return &dto.User{ 30 | Id: 1, 31 | Name: name, 32 | Age: age, 33 | }, nil 34 | } 35 | -------------------------------------------------------------------------------- /example/autowire/autowire_rpc/server/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "github.com/alibaba/ioc-golang" 20 | _ "github.com/alibaba/ioc-golang/example/autowire/autowire_rpc/server/pkg/service" 21 | ) 22 | 23 | func main() { 24 | // start 25 | if err := ioc.Load(); err != nil { 26 | panic(err) 27 | } 28 | select {} 29 | } 30 | -------------------------------------------------------------------------------- /example/autowire/autowire_rpc/server/pkg/dto/user.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package dto 17 | 18 | type User struct { 19 | Id int 20 | Name string 21 | Age int 22 | } 23 | -------------------------------------------------------------------------------- /example/autowire/autowire_rpc/server/pkg/service/api/zz_generated.ioc_rpc_client_servicestruct.go: -------------------------------------------------------------------------------- 1 | // Code generated by iocli, run 'iocli gen' to re-generate 2 | 3 | package api 4 | 5 | import ( 6 | "github.com/alibaba/ioc-golang/autowire" 7 | "github.com/alibaba/ioc-golang/autowire/normal" 8 | "github.com/alibaba/ioc-golang/example/autowire/autowire_rpc/server/pkg/dto" 9 | "github.com/alibaba/ioc-golang/extension/autowire/rpc/rpc_client" 10 | ) 11 | 12 | func init() { 13 | rpc_client.RegisterStructDescriptor(&autowire.StructDescriptor{ 14 | Factory: func() interface{} { 15 | return &serviceStructIOCRPCClient{} 16 | }, 17 | Metadata: map[string]interface{}{ 18 | "aop": map[string]interface{}{}, 19 | "autowire": map[string]interface{}{}, 20 | }, 21 | }) 22 | normal.RegisterStructDescriptor(&autowire.StructDescriptor{ 23 | Factory: func() interface{} { 24 | return &serviceStructIOCRPCClient_{} 25 | }, 26 | }) 27 | } 28 | 29 | type serviceStructIOCRPCClient_ struct { 30 | GetUser_ func(name string, age int) (*dto.User, error) 31 | } 32 | 33 | func (s *serviceStructIOCRPCClient_) GetUser(name string, age int) (*dto.User, error) { 34 | return s.GetUser_(name, age) 35 | } 36 | 37 | type ServiceStructIOCRPCClient interface { 38 | GetUser(name string, age int) (*dto.User, error) 39 | } 40 | 41 | type serviceStructIOCRPCClient struct { 42 | GetUser func(name string, age int) (*dto.User, error) 43 | } 44 | -------------------------------------------------------------------------------- /example/autowire/autowire_rpc/server/pkg/service/service.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package service 17 | 18 | import ( 19 | "github.com/alibaba/ioc-golang/example/autowire/autowire_rpc/server/pkg/dto" 20 | ) 21 | 22 | // +ioc:autowire=true 23 | // +ioc:autowire:type=rpc 24 | 25 | type ServiceStruct struct { 26 | } 27 | 28 | func (s *ServiceStruct) GetUser(name string, age int) (*dto.User, error) { 29 | return &dto.User{ 30 | Id: 1, 31 | Name: name, 32 | Age: age, 33 | }, nil 34 | } 35 | -------------------------------------------------------------------------------- /example/autowire/get_impl_by_api/docker-compose/docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: '3.1' 2 | services: 3 | get_by_api: 4 | image: redis:latest 5 | container_name: get_by_api-server 6 | ports: 7 | - "6379:6379" -------------------------------------------------------------------------------- /example/config_file/README.md: -------------------------------------------------------------------------------- 1 | # 配置文件加载例子 2 | 3 | - activete_profile 使用以特定 profile 为后缀的配置文件 4 | - complex_example 一个覆盖多种 API 的配置加载例子 5 | - config_by_api 一个基于 API 的方式写入配置的例子 6 | - default_config_file 框架默认加载配置文件的例子 7 | - mark_env_variable_in_config_file 在配置文件中引用环境变量 8 | - mark_nested_value_in_config_file 载配置文件中引入配置文件内特定位置的值 9 | - set_config_file_search_path 设置配置文件扫描路径 10 | - set_config_file_type 设置配置文件类型:yaml/yml 11 | - set_config_name 指定配置文件名 -------------------------------------------------------------------------------- /example/config_file/active_profile/config/config_dev.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: myValue -------------------------------------------------------------------------------- /example/config_file/active_profile/config/config_pro.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: notMyValue -------------------------------------------------------------------------------- /example/config_file/active_profile/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | 21 | "github.com/alibaba/ioc-golang" 22 | "github.com/alibaba/ioc-golang/config" 23 | configField "github.com/alibaba/ioc-golang/extension/config" 24 | ) 25 | 26 | // +ioc:autowire=true 27 | // +ioc:autowire:type=singleton 28 | 29 | type App struct { 30 | ConfigValue *configField.ConfigString `config:",config.app.config-value"` 31 | } 32 | 33 | func (a *App) Run() { 34 | fmt.Printf("Load '%s' from config file\n", a.ConfigValue.Value()) 35 | } 36 | 37 | func main() { 38 | // config.WithProfilesActive or set env 'IOC_GOLANG_CONFIG_ACTIVE_PROFILE' 39 | if err := ioc.Load(config.WithProfilesActive("dev")); err != nil { 40 | panic(err) 41 | } 42 | 43 | app, err := GetAppIOCInterfaceSingleton() 44 | if err != nil { 45 | panic(err) 46 | } 47 | app.Run() 48 | } 49 | -------------------------------------------------------------------------------- /example/config_file/active_profile/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/assert" 8 | 9 | "github.com/alibaba/ioc-golang" 10 | "github.com/alibaba/ioc-golang/config" 11 | ) 12 | 13 | func (a *App) TestRun(t *testing.T) { 14 | assert.Equal(t, "myValue", a.ConfigValue.Value()) 15 | } 16 | 17 | func TestSetConfigActiveProfileByOption(t *testing.T) { 18 | // start 19 | if err := ioc.Load(config.WithProfilesActive("dev")); err != nil { 20 | panic(err) 21 | } 22 | 23 | app, err := GetAppSingleton() 24 | if err != nil { 25 | panic(err) 26 | } 27 | app.TestRun(t) 28 | } 29 | 30 | func TestSetConfigActiveProfileByEnv(t *testing.T) { 31 | os.Setenv("IOC_GOLANG_CONFIG_ACTIVE_PROFILE", "dev") 32 | if err := ioc.Load(); err != nil { 33 | panic(err) 34 | } 35 | 36 | app, err := GetAppSingleton() 37 | if err != nil { 38 | panic(err) 39 | } 40 | app.TestRun(t) 41 | } 42 | -------------------------------------------------------------------------------- /example/config_file/complex_example/docker-compose/docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: '3.1' 2 | services: 3 | demo-redis: 4 | image: redis:latest 5 | container_name: demo-redis 6 | ports: 7 | - "6379:6379" -------------------------------------------------------------------------------- /example/config_file/complex_example/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/assert" 8 | 9 | "github.com/alibaba/ioc-golang" 10 | "github.com/alibaba/ioc-golang/config" 11 | "github.com/alibaba/ioc-golang/test/docker_compose" 12 | ) 13 | 14 | func (a *App) TestRun(t *testing.T) { 15 | assert.Equal(t, "myValue", a.ConfigValue.Value()) 16 | assert.Equal(t, "myEnvValue", a.ConfigValueFromEnv.Value()) 17 | assert.Equal(t, "myValue", a.NestedConfigValue.Value()) 18 | assert.Equal(t, "myEnvValue", a.NestedConfigValueFromEnv.Value()) 19 | assert.Equal(t, "myEnvValue", a.ConfigValueFromTagEnvKey.Value()) 20 | assert.Nil(t, a.RedisClient.Ping().Err()) 21 | } 22 | 23 | func TestSetConfigName(t *testing.T) { 24 | assert.Nil(t, docker_compose.DockerComposeUp("./docker-compose/docker-compose.yaml", 5)) 25 | // start 26 | os.Setenv("MY_CONFIG_ENV_KEY", "myEnvValue") 27 | os.Setenv("REDIS_ADDRESS", "127.0.0.1:6379") 28 | if err := ioc.Load( 29 | config.WithSearchPath("./redis_config", "./my_custom_config_path"), 30 | config.WithProfilesActive("pro")); err != nil { 31 | panic(err) 32 | } 33 | 34 | app, err := GetAppSingleton() 35 | if err != nil { 36 | panic(err) 37 | } 38 | app.TestRun(t) 39 | assert.Nil(t, docker_compose.DockerComposeDown("./docker-compose/docker-compose.yaml")) 40 | } 41 | -------------------------------------------------------------------------------- /example/config_file/complex_example/my_custom_config_path/config_dev.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: notMyValue 4 | config-value-from-env: not${MY_CONFIG_ENV_KEY} 5 | nested-config-value: ${config.app.config-value} 6 | nested-config-value-from-env: ${config.app.config-value-from-env} -------------------------------------------------------------------------------- /example/config_file/complex_example/my_custom_config_path/config_pro.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: myValue 4 | config-value-from-env: ${MY_CONFIG_ENV_KEY} 5 | nested-config-value: ${config.app.config-value} 6 | nested-config-value-from-env: ${config.app.config-value-from-env} -------------------------------------------------------------------------------- /example/config_file/complex_example/redis_config/config_dev.yaml: -------------------------------------------------------------------------------- 1 | autowire: 2 | singleton: 3 | github.com/alibaba/ioc-golang/extension/state/redis.Redis: 4 | param: 5 | address: bad-host:6379 -------------------------------------------------------------------------------- /example/config_file/complex_example/redis_config/config_pro.yaml: -------------------------------------------------------------------------------- 1 | autowire: 2 | singleton: 3 | github.com/alibaba/ioc-golang/extension/state/redis.Redis: 4 | param: 5 | address: ${REDIS_ADDRESS} 6 | -------------------------------------------------------------------------------- /example/config_file/config_by_api/docker-compose/docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: '3.1' 2 | services: 3 | demo-redis: 4 | image: redis:latest 5 | container_name: demo-redis 6 | ports: 7 | - "6379:6379" -------------------------------------------------------------------------------- /example/config_file/default_config_file/config/config.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: myValue -------------------------------------------------------------------------------- /example/config_file/default_config_file/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | 21 | "github.com/alibaba/ioc-golang" 22 | configField "github.com/alibaba/ioc-golang/extension/config" 23 | ) 24 | 25 | // +ioc:autowire=true 26 | // +ioc:autowire:type=singleton 27 | 28 | type App struct { 29 | ConfigValue *configField.ConfigString `config:",config.app.config-value"` 30 | } 31 | 32 | func (a *App) Run() { 33 | fmt.Printf("Load '%s' from config file\n", a.ConfigValue.Value()) 34 | } 35 | 36 | func main() { 37 | // start 38 | if err := ioc.Load(); err != nil { 39 | panic(err) 40 | } 41 | 42 | app, err := GetAppIOCInterfaceSingleton() 43 | if err != nil { 44 | panic(err) 45 | } 46 | app.Run() 47 | } 48 | -------------------------------------------------------------------------------- /example/config_file/default_config_file/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | 8 | "github.com/alibaba/ioc-golang" 9 | ) 10 | 11 | func (a *App) TestRun(t *testing.T) { 12 | assert.Equal(t, "myValue", a.ConfigValue.Value()) 13 | } 14 | 15 | func TestSetConfigName(t *testing.T) { 16 | // start 17 | if err := ioc.Load(); err != nil { 18 | panic(err) 19 | } 20 | 21 | app, err := GetAppSingleton() 22 | if err != nil { 23 | panic(err) 24 | } 25 | app.TestRun(t) 26 | } 27 | -------------------------------------------------------------------------------- /example/config_file/mark_env_variable_in_config_file/config/config.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: myValue 4 | config-value-from-env: ${MY_CONFIG_ENV_KEY} -------------------------------------------------------------------------------- /example/config_file/mark_env_variable_in_config_file/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | "os" 21 | 22 | "github.com/alibaba/ioc-golang" 23 | configField "github.com/alibaba/ioc-golang/extension/config" 24 | ) 25 | 26 | // +ioc:autowire=true 27 | // +ioc:autowire:type=singleton 28 | 29 | type App struct { 30 | ConfigValue *configField.ConfigString `config:",config.app.config-value"` 31 | ConfigValueFromEnv *configField.ConfigString `config:",config.app.config-value-from-env"` 32 | } 33 | 34 | func (a *App) Run() { 35 | fmt.Printf("Load '%s' from config file\n", a.ConfigValue.Value()) 36 | fmt.Printf("Load '%s' from env\n", a.ConfigValueFromEnv.Value()) 37 | } 38 | 39 | func main() { 40 | // start 41 | os.Setenv("MY_CONFIG_ENV_KEY", "myEnvValue") 42 | if err := ioc.Load(); err != nil { 43 | panic(err) 44 | } 45 | 46 | app, err := GetAppIOCInterfaceSingleton() 47 | if err != nil { 48 | panic(err) 49 | } 50 | app.Run() 51 | } 52 | -------------------------------------------------------------------------------- /example/config_file/mark_env_variable_in_config_file/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/assert" 8 | 9 | "github.com/alibaba/ioc-golang" 10 | ) 11 | 12 | func (a *App) TestRun(t *testing.T) { 13 | assert.Equal(t, "myValue", a.ConfigValue.Value()) 14 | assert.Equal(t, "myEnvValue", a.ConfigValueFromEnv.Value()) 15 | } 16 | 17 | func TestSetConfigName(t *testing.T) { 18 | // start 19 | os.Setenv("MY_CONFIG_ENV_KEY", "myEnvValue") 20 | if err := ioc.Load(); err != nil { 21 | panic(err) 22 | } 23 | 24 | app, err := GetAppSingleton() 25 | if err != nil { 26 | panic(err) 27 | } 28 | app.TestRun(t) 29 | } 30 | -------------------------------------------------------------------------------- /example/config_file/mark_nested_value_in_config_file/config/config.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: myValue 4 | config-value-from-env: ${MY_CONFIG_ENV_KEY} 5 | nested-config-value: ${config.app.config-value} 6 | nested-config-value-from-env: ${config.app.config-value-from-env} -------------------------------------------------------------------------------- /example/config_file/mark_nested_value_in_config_file/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/assert" 8 | 9 | "github.com/alibaba/ioc-golang" 10 | ) 11 | 12 | func (a *App) TestRun(t *testing.T) { 13 | assert.Equal(t, "myValue", a.ConfigValue.Value()) 14 | assert.Equal(t, "myEnvValue", a.ConfigValueFromEnv.Value()) 15 | assert.Equal(t, "myValue", a.NestedConfigValue.Value()) 16 | assert.Equal(t, "myEnvValue", a.NestedConfigValueFromEnv.Value()) 17 | } 18 | 19 | func TestSetConfigName(t *testing.T) { 20 | // start 21 | os.Setenv("MY_CONFIG_ENV_KEY", "myEnvValue") 22 | if err := ioc.Load(); err != nil { 23 | panic(err) 24 | } 25 | 26 | app, err := GetAppSingleton() 27 | if err != nil { 28 | panic(err) 29 | } 30 | app.TestRun(t) 31 | } 32 | -------------------------------------------------------------------------------- /example/config_file/set_config_file_search_path/config/config.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: notMyValue -------------------------------------------------------------------------------- /example/config_file/set_config_file_search_path/custom_config_path/config.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: myValue -------------------------------------------------------------------------------- /example/config_file/set_config_file_search_path/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | 21 | "github.com/alibaba/ioc-golang" 22 | "github.com/alibaba/ioc-golang/config" 23 | configField "github.com/alibaba/ioc-golang/extension/config" 24 | ) 25 | 26 | // +ioc:autowire=true 27 | // +ioc:autowire:type=singleton 28 | 29 | type App struct { 30 | ConfigValue *configField.ConfigString `config:",config.app.config-value"` 31 | } 32 | 33 | func (a *App) Run() { 34 | fmt.Printf("Load '%s' from config file\n", a.ConfigValue.Value()) 35 | } 36 | 37 | func main() { 38 | // config.WithSearchPath or set env 'IOC_GOLANG_CONFIG_SEARCH_PATH' 39 | if err := ioc.Load(config.WithSearchPath("./custom_config_path")); err != nil { 40 | panic(err) 41 | } 42 | 43 | app, err := GetAppIOCInterfaceSingleton() 44 | if err != nil { 45 | panic(err) 46 | } 47 | app.Run() 48 | } 49 | -------------------------------------------------------------------------------- /example/config_file/set_config_file_search_path/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/assert" 8 | 9 | "github.com/alibaba/ioc-golang" 10 | "github.com/alibaba/ioc-golang/config" 11 | ) 12 | 13 | func (a *App) TestRun(t *testing.T) { 14 | assert.Equal(t, "myValue", a.ConfigValue.Value()) 15 | } 16 | 17 | func TestSetConfigSearchPathByOption(t *testing.T) { 18 | // start 19 | if err := ioc.Load(config.WithSearchPath("./custom_config_path")); err != nil { 20 | panic(err) 21 | } 22 | 23 | app, err := GetAppSingleton() 24 | if err != nil { 25 | panic(err) 26 | } 27 | app.TestRun(t) 28 | } 29 | 30 | func TestSetConfigSearchPathByEnv(t *testing.T) { 31 | // start 32 | os.Setenv("IOC_GOLANG_CONFIG_SEARCH_PATH", "./custom_config_path") 33 | if err := ioc.Load(); err != nil { 34 | panic(err) 35 | } 36 | 37 | app, err := GetAppSingleton() 38 | if err != nil { 39 | panic(err) 40 | } 41 | app.TestRun(t) 42 | } 43 | -------------------------------------------------------------------------------- /example/config_file/set_config_file_type/config/config.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: notMyValue -------------------------------------------------------------------------------- /example/config_file/set_config_file_type/config/config.yml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: myValue -------------------------------------------------------------------------------- /example/config_file/set_config_file_type/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | 21 | "github.com/alibaba/ioc-golang" 22 | "github.com/alibaba/ioc-golang/config" 23 | configField "github.com/alibaba/ioc-golang/extension/config" 24 | ) 25 | 26 | // +ioc:autowire=true 27 | // +ioc:autowire:type=singleton 28 | 29 | type App struct { 30 | ConfigValue *configField.ConfigString `config:",config.app.config-value"` 31 | } 32 | 33 | func (a *App) Run() { 34 | fmt.Printf("Load '%s' from config file\n", a.ConfigValue.Value()) 35 | } 36 | 37 | func main() { 38 | // config.WithConfigType or set env 'IOC_GOLANG_CONFIG_TYPE' 39 | if err := ioc.Load(config.WithConfigType("yml")); err != nil { 40 | panic(err) 41 | } 42 | 43 | app, err := GetAppIOCInterfaceSingleton() 44 | if err != nil { 45 | panic(err) 46 | } 47 | app.Run() 48 | } 49 | -------------------------------------------------------------------------------- /example/config_file/set_config_file_type/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/assert" 8 | 9 | "github.com/alibaba/ioc-golang" 10 | "github.com/alibaba/ioc-golang/config" 11 | ) 12 | 13 | func (a *App) TestRun(t *testing.T) { 14 | assert.Equal(t, "myValue", a.ConfigValue.Value()) 15 | } 16 | 17 | func TestSetConfigTypeByOption(t *testing.T) { 18 | // start 19 | if err := ioc.Load(config.WithConfigType("yml")); err != nil { 20 | panic(err) 21 | } 22 | 23 | app, err := GetAppSingleton() 24 | if err != nil { 25 | panic(err) 26 | } 27 | app.TestRun(t) 28 | } 29 | 30 | func TestSetConfigTypeByEnv(t *testing.T) { 31 | os.Setenv("IOC_GOLANG_CONFIG_TYPE", "yml") 32 | if err := ioc.Load(); err != nil { 33 | panic(err) 34 | } 35 | 36 | app, err := GetAppSingleton() 37 | if err != nil { 38 | panic(err) 39 | } 40 | app.TestRun(t) 41 | } 42 | -------------------------------------------------------------------------------- /example/config_file/set_config_name/config/config.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: notMyValue -------------------------------------------------------------------------------- /example/config_file/set_config_name/config/custom_config.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | app: 3 | config-value: myValue -------------------------------------------------------------------------------- /example/config_file/set_config_name/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | 21 | "github.com/alibaba/ioc-golang" 22 | "github.com/alibaba/ioc-golang/config" 23 | configField "github.com/alibaba/ioc-golang/extension/config" 24 | ) 25 | 26 | // +ioc:autowire=true 27 | // +ioc:autowire:type=singleton 28 | 29 | type App struct { 30 | ConfigValue *configField.ConfigString `config:",config.app.config-value"` 31 | } 32 | 33 | func (a *App) Run() { 34 | fmt.Printf("Load '%s' from config file\n", a.ConfigValue.Value()) 35 | } 36 | 37 | func main() { 38 | // config.WithConfigName or set env 'IOC_GOLANG_CONFIG_NAME' 39 | if err := ioc.Load(config.WithConfigName("custom_config")); err != nil { 40 | panic(err) 41 | } 42 | 43 | app, err := GetAppIOCInterfaceSingleton() 44 | if err != nil { 45 | panic(err) 46 | } 47 | app.Run() 48 | } 49 | -------------------------------------------------------------------------------- /example/config_file/set_config_name/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/assert" 8 | 9 | "github.com/alibaba/ioc-golang" 10 | "github.com/alibaba/ioc-golang/config" 11 | ) 12 | 13 | func (a *App) TestRun(t *testing.T) { 14 | assert.Equal(t, "myValue", a.ConfigValue.Value()) 15 | } 16 | 17 | func TestSetConfigNameByOption(t *testing.T) { 18 | // start 19 | if err := ioc.Load(config.WithConfigName("custom_config")); err != nil { 20 | panic(err) 21 | } 22 | 23 | app, err := GetAppSingleton() 24 | if err != nil { 25 | panic(err) 26 | } 27 | app.TestRun(t) 28 | } 29 | 30 | func TestSetConfigNameByEnv(t *testing.T) { 31 | // start 32 | os.Setenv("IOC_GOALNG_CONFIG_NAME", "custom_config") 33 | if err := ioc.Load(); err != nil { 34 | panic(err) 35 | } 36 | 37 | app, err := GetAppSingleton() 38 | if err != nil { 39 | panic(err) 40 | } 41 | app.TestRun(t) 42 | } 43 | -------------------------------------------------------------------------------- /example/helloworld/README_CN.md: -------------------------------------------------------------------------------- 1 | # Helloworld 示例 2 | 3 | 本示例展示了 [README](https://github.com/alibaba/IOC-golang/blob/master/README_CN.md) 中给出的的 Demo。 -------------------------------------------------------------------------------- /example/third_party/autowire/grpc/api/hello.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | /* 4 | * Copyright (c) 2022, Alibaba Group; 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package api; 19 | 20 | option go_package = "./;api"; 21 | 22 | service HelloService { 23 | rpc SayHello (HelloRequest) returns (HelloResponse) {} 24 | } 25 | 26 | message HelloRequest { 27 | string name = 1; 28 | } 29 | 30 | message HelloResponse { 31 | string reply = 1; 32 | } 33 | -------------------------------------------------------------------------------- /example/third_party/autowire/grpc/cmd/service1/service1_impl1.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package service1 17 | 18 | import ( 19 | "context" 20 | 21 | "github.com/alibaba/ioc-golang/example/third_party/autowire/grpc/api" 22 | ) 23 | 24 | // +ioc:autowire=true 25 | // +ioc:autowire:type=singleton 26 | type Impl1 struct { 27 | HelloServiceClient api.HelloServiceClient `grpc:"hello-service"` 28 | } 29 | 30 | func (i *Impl1) Hello(req string) string { 31 | rsp, err := i.HelloServiceClient.SayHello(context.Background(), &api.HelloRequest{ 32 | Name: req, 33 | }) 34 | if err != nil { 35 | panic(err) 36 | } 37 | return rsp.Reply 38 | } 39 | -------------------------------------------------------------------------------- /example/third_party/autowire/grpc/cmd/service1/service1_interface.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package service1 17 | 18 | type Service1 interface { 19 | Hello(string) string 20 | } 21 | -------------------------------------------------------------------------------- /example/third_party/autowire/grpc/cmd/service2/service2_impl1.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package service2 17 | 18 | import ( 19 | "context" 20 | 21 | "github.com/alibaba/ioc-golang/example/third_party/autowire/grpc/api" 22 | ) 23 | 24 | // +ioc:autowire=true 25 | // +ioc:autowire:type=singleton 26 | type Impl1 struct { 27 | HelloServiceClient api.HelloServiceClient `grpc:"hello-service"` 28 | } 29 | 30 | func (i *Impl1) Hello(name string) string { 31 | rsp, err := i.HelloServiceClient.SayHello(context.Background(), &api.HelloRequest{ 32 | Name: name, 33 | }) 34 | if err != nil { 35 | panic(err) 36 | } 37 | return rsp.Reply 38 | } 39 | -------------------------------------------------------------------------------- /example/third_party/autowire/grpc/cmd/service2/service2_impl2.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package service2 17 | 18 | import ( 19 | "context" 20 | 21 | "github.com/alibaba/ioc-golang/example/third_party/autowire/grpc/api" 22 | ) 23 | 24 | // +ioc:autowire=true 25 | // +ioc:autowire:type=singleton 26 | type Impl2 struct { 27 | HelloServiceClient api.HelloServiceClient `grpc:"hello-service"` 28 | } 29 | 30 | func (i *Impl2) Hello(name string) string { 31 | rsp, err := i.HelloServiceClient.SayHello(context.Background(), &api.HelloRequest{ 32 | Name: name, 33 | }) 34 | if err != nil { 35 | panic(err) 36 | } 37 | return rsp.Reply 38 | } 39 | -------------------------------------------------------------------------------- /example/third_party/autowire/grpc/cmd/service2/service2_interface.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package service2 17 | 18 | type Service2 interface { 19 | Hello(string) string 20 | } 21 | -------------------------------------------------------------------------------- /example/third_party/autowire/grpc/cmd/struct1/service1_impl1.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package struct1 17 | 18 | import ( 19 | "context" 20 | 21 | "github.com/alibaba/ioc-golang/example/third_party/autowire/grpc/api" 22 | ) 23 | 24 | // +ioc:autowire=true 25 | // +ioc:autowire:type=singleton 26 | type Struct1 struct { 27 | HelloServiceClient api.HelloServiceClient `grpc:"hello-service"` 28 | } 29 | 30 | func (i *Struct1) Hello(name string) string { 31 | rsp, err := i.HelloServiceClient.SayHello(context.Background(), &api.HelloRequest{ 32 | Name: name, 33 | }) 34 | if err != nil { 35 | panic(err) 36 | } 37 | return rsp.Reply 38 | } 39 | -------------------------------------------------------------------------------- /example/third_party/autowire/grpc/conf/config.yaml: -------------------------------------------------------------------------------- 1 | autowire: 2 | grpc: 3 | hello-service: 4 | address: localhost:8080 -------------------------------------------------------------------------------- /example/third_party/autowire/grpc/docker-compose/docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: '3.1' 2 | services: 3 | grpc_server: 4 | image: laurencelizhixin/grpc-server-sayhello:1.0.0 5 | container_name: grpc-server 6 | ports: 7 | - "8080:8080" -------------------------------------------------------------------------------- /example/third_party/autowire/grpc/grpc_server/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3.15 2 | 3 | ARG TARGETARCH 4 | 5 | WORKDIR /server 6 | COPY "./app_${TARGETARCH}" /server/app 7 | 8 | ENTRYPOINT /server/app 9 | -------------------------------------------------------------------------------- /example/third_party/autowire/grpc/grpc_server/Makefile: -------------------------------------------------------------------------------- 1 | IMAGE = laurencelizhixin/grpc-server-sayhello 2 | TAG = 1.0.0 3 | 4 | buildx-publish: 5 | GOARCH=amd64 GOOS=linux go build -o app_amd64 . 6 | GOARCH=arm64 GOOS=linux go build -o app_arm64 . 7 | docker buildx build \ 8 | --platform linux/amd64,linux/arm64 \ 9 | -t ${IMAGE}:${TAG} \ 10 | . --push 11 | rm ./app* -------------------------------------------------------------------------------- /example/third_party/autowire/grpc/grpc_server/server.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "context" 20 | "net" 21 | 22 | "google.golang.org/grpc" 23 | 24 | "github.com/alibaba/ioc-golang/example/third_party/autowire/grpc/api" 25 | ) 26 | 27 | type HelloServiceImpl struct { 28 | api.UnimplementedHelloServiceServer 29 | } 30 | 31 | func (h *HelloServiceImpl) SayHello(_ context.Context, req *api.HelloRequest) (*api.HelloResponse, error) { 32 | return &api.HelloResponse{ 33 | Reply: "Hello " + req.Name, 34 | }, nil 35 | } 36 | 37 | func main() { 38 | lst, err := net.Listen("tcp", ":8080") 39 | if err != nil { 40 | panic(err) 41 | } 42 | grpcServer := grpc.NewServer() 43 | grpcServer.RegisterService(&api.HelloService_ServiceDesc, &HelloServiceImpl{}) 44 | if err := grpcServer.Serve(lst); err != nil { 45 | panic(err) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /example/third_party/db/gorm/conf/config.yaml: -------------------------------------------------------------------------------- 1 | autowire: 2 | normal: 3 | github.com/alibaba/ioc-golang/extension/db/gorm.GORMDB: 4 | my-mysql: 5 | param: 6 | host: "127.0.0.1" 7 | port: 3306 8 | username: "root" 9 | password: "root" 10 | dbname: "test" -------------------------------------------------------------------------------- /example/third_party/db/gorm/docker-compose/docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: '3.1' 2 | services: 3 | mysql: 4 | image: mysql:latest 5 | restart: always 6 | container_name: demo-mysql 7 | environment: 8 | MYSQL_ROOT_PASSWORD: root 9 | MYSQL_DATABASE: test 10 | ports: 11 | - "3306:3306" -------------------------------------------------------------------------------- /example/third_party/pubsub/rocketmq/conf/config.yaml: -------------------------------------------------------------------------------- 1 | autowire: 2 | singleton: 3 | github.com/alibaba/ioc-golang/extension/pubsub/rocketmq.Producer: 4 | param: 5 | groupname: default2 6 | nameserver: 7 | - 127.0.0.1:9876 8 | github.com/alibaba/ioc-golang/extension/pubsub/rocketmq.PushConsumer: 9 | param: ${autowire.singleton..param} 10 | github.com/alibaba/ioc-golang/extension/pubsub/rocketmq.Admin: 11 | param: 12 | nameserver: 13 | - 127.0.0.1:9876 14 | -------------------------------------------------------------------------------- /example/third_party/registry/nacos/conf/config.yaml: -------------------------------------------------------------------------------- 1 | autowire: 2 | normal: 3 | github.com/alibaba/ioc-golang/extension/registry/nacos.NamingClient: 4 | nacos-2: 5 | param: 6 | nacosclientparam: 7 | clientconfig: 8 | appKey: appKey 9 | serverconfigs: 10 | - ipaddr: 127.0.0.1 11 | port: 8848 12 | param: 13 | nacosclientparam: 14 | clientconfig: 15 | appKey: appKey 16 | serverconfigs: 17 | - ipaddr: 127.0.0.1 18 | port: 8848 -------------------------------------------------------------------------------- /example/third_party/registry/nacos/docker-compose/docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | nacos: 4 | image: nacos/nacos-server:2.0.3-slim 5 | container_name: nacos-standalone 6 | environment: 7 | - PREFER_HOST_MODE=hostname 8 | - MODE=standalone 9 | ports: 10 | - "8848:8848" 11 | - "9848:9848" -------------------------------------------------------------------------------- /example/third_party/state/redis/conf/config.yaml: -------------------------------------------------------------------------------- 1 | autowire: 2 | normal: 3 | github.com/alibaba/ioc-golang/extension/state/redis.Redis: 4 | db1-redis: 5 | param: 6 | address: localhost:6379 7 | db: 1 8 | db2-redis: 9 | param: 10 | address: localhost:6379 11 | db: 2 12 | param: 13 | address: localhost:6379 14 | db: 0 15 | expand: 16 | address: ${REDIS_ADDRESS_EXPAND} 17 | db: 15 18 | nested: 19 | address: ${autowire.normal..expand.address} 20 | db: ${autowire.normal..expand.db} -------------------------------------------------------------------------------- /example/third_party/state/redis/docker-compose/docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: '3.1' 2 | services: 3 | demo-redis: 4 | image: redis:latest 5 | container_name: demo-redis 6 | ports: 7 | - "6379:6379" -------------------------------------------------------------------------------- /extension/README.md: -------------------------------------------------------------------------------- 1 | # IOC-golang 扩展组件 2 | 3 | ### [English](./README_EN.md) | 中文 4 | 5 | ### 基于 ‘自动装载模型’ 和 AOP 能力的扩展 6 | 7 | - aop/ 8 | 9 | 提供了基于 AOP 的多种扩展能力,包括与之相关的覆盖 RPC、AOP 拦截器、Debug 服务、cli 命令的相关实现。 10 | - call 11 | 12 | 使用 iocli 工具调用接口方法的能力 13 | 14 | - list/ 15 | 16 | 接口方法可视化能力 17 | 18 | - watch/ 19 | 20 | 接口、方法、实时参数观测能力 21 | 22 | - monitor/ 23 | 24 | 实时调用监控能力 25 | 26 | - trace/ 27 | 28 | 链路追踪、分布式链路追踪、链路日志采集能力 29 | 30 | - transaction/ 31 | 32 | 事务、分布式事务能力 33 | 34 | - dynamic_plugin 35 | 36 | 基于 Go 原生插件机制的接口动态代理能力 37 | 38 | - autowire/ 39 | 40 | 提供了自动装载模型的扩展 41 | 42 | - allimpls/ 43 | 44 | 定义了注入一个接口的全部实现的模型。 45 | 46 | - config/ 47 | 48 | 定义配置字段自动装载模型 49 | 50 | - rpc/ 51 | 52 | 定义了 ioc-golang 原生支持的 RPC 模型,覆盖 client、server 端。 53 | 54 | - grpc/ 55 | 56 | gRPC 客户端自动装载模型 57 | 58 | ### 第三方预置组件扩展 59 | 60 | 为方便开发者直接注入,IOC-golang 提供了覆盖多个领域的预置组件,可供开发者直接注入使用。 61 | 62 | - config/ 63 | 64 | 提供了可以基于配置文件注入的基本数据类型 65 | 66 | - config_center/ 67 | 68 | 提供了可以直接注入的配置中心客户端结构 69 | 70 | - nacos 71 | 72 | - db/ 73 | 74 | 提供了可以操作数据库的客户端结构 75 | 76 | - gorm 77 | 78 | - pubsub/ 79 | 80 | 提供了可以操作消息队列的客户端结构 81 | 82 | - rocketmq 83 | 84 | - registry/ 85 | 86 | 提供了可以操作分布式场景下注册中心的客户端结构 87 | 88 | - nacos 89 | 90 | - state/ 91 | 92 | 提供了可以保存状态、缓存的客户端结构 93 | 94 | - redis 95 | 96 | -------------------------------------------------------------------------------- /extension/aop/call/aop.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package call 17 | 18 | import ( 19 | "google.golang.org/grpc" 20 | 21 | "github.com/alibaba/ioc-golang/aop" 22 | "github.com/alibaba/ioc-golang/extension/aop/call/api/ioc_golang/aop/call" 23 | ) 24 | 25 | const Name = "call" 26 | 27 | func init() { 28 | aop.RegisterAOP(aop.AOP{ 29 | Name: Name, 30 | GRPCServiceRegister: func(server *grpc.Server) { 31 | callServiceImplSingleton, _ := GetcallServiceImplSingleton() 32 | call.RegisterCallServiceServer(server, callServiceImplSingleton) 33 | }, 34 | }) 35 | } 36 | -------------------------------------------------------------------------------- /extension/aop/call/api/ioc_golang/aop/call/call.proto: -------------------------------------------------------------------------------- 1 | // EDIT IT, change to your package, service and message 2 | syntax = "proto3"; 3 | /* 4 | * Copyright (c) 2022, Alibaba Group; 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package ioc_golang.aop.call; 19 | 20 | option go_package = "ioc_golang/aop/call"; 21 | 22 | service CallService { 23 | rpc Call (CallRequest) returns (CallResponse) {} 24 | } 25 | 26 | message CallRequest{ 27 | string sdid = 1; 28 | string methodName = 2; 29 | string autowireType = 3; 30 | bytes params = 4; 31 | } 32 | 33 | message CallResponse{ 34 | string sdid = 1; 35 | string methodName = 2; 36 | bytes params = 3; 37 | bytes returnValues = 4; 38 | } -------------------------------------------------------------------------------- /extension/aop/call/zz_generated.ioc.go: -------------------------------------------------------------------------------- 1 | //go:build !ignore_autogenerated 2 | // +build !ignore_autogenerated 3 | 4 | // Code generated by iocli, run 'iocli gen' to re-generate 5 | 6 | package call 7 | 8 | import ( 9 | "github.com/alibaba/ioc-golang/autowire" 10 | singleton "github.com/alibaba/ioc-golang/autowire/singleton" 11 | util "github.com/alibaba/ioc-golang/autowire/util" 12 | ) 13 | 14 | func init() { 15 | callServiceImplStructDescriptor := &autowire.StructDescriptor{ 16 | Factory: func() interface{} { 17 | return &callServiceImpl{} 18 | }, 19 | Metadata: map[string]interface{}{ 20 | "aop": map[string]interface{}{}, 21 | "autowire": map[string]interface{}{}, 22 | }, 23 | DisableProxy: true, 24 | } 25 | singleton.RegisterStructDescriptor(callServiceImplStructDescriptor) 26 | } 27 | 28 | var _callServiceImplSDID string 29 | 30 | func GetcallServiceImplSingleton() (*callServiceImpl, error) { 31 | if _callServiceImplSDID == "" { 32 | _callServiceImplSDID = util.GetSDIDByStructPtr(new(callServiceImpl)) 33 | } 34 | i, err := singleton.GetImpl(_callServiceImplSDID, nil) 35 | if err != nil { 36 | return nil, err 37 | } 38 | impl := i.(*callServiceImpl) 39 | return impl, nil 40 | } 41 | -------------------------------------------------------------------------------- /extension/aop/dynamic_plugin/aop.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package dynamic_plugin 17 | 18 | import ( 19 | "google.golang.org/grpc" 20 | 21 | "github.com/alibaba/ioc-golang/aop" 22 | "github.com/alibaba/ioc-golang/extension/aop/dynamic_plugin/api/ioc_golang/aop/dynamic_plugin" 23 | ) 24 | 25 | const Name = "dynamic_plugin" 26 | 27 | func init() { 28 | aop.RegisterAOP(aop.AOP{ 29 | Name: Name, 30 | GRPCServiceRegister: func(server *grpc.Server) { 31 | listServiceImplSingleton, _ := GetdynamicPluginServiceImplSingleton() 32 | dynamic_plugin.RegisterDynamicPluginServiceServer(server, listServiceImplSingleton) 33 | }, 34 | }) 35 | } 36 | -------------------------------------------------------------------------------- /extension/aop/dynamic_plugin/api/ioc_golang/aop/dynamic_plugin/dynamic_plugin.proto: -------------------------------------------------------------------------------- 1 | // EDIT IT, change to your package, service and message 2 | syntax = "proto3"; 3 | /* 4 | * Copyright (c) 2022, Alibaba Group; 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package ioc_golang.aop.dynamic_plugin; 19 | 20 | option go_package = "ioc_golang/aop/dynamic_plugin"; 21 | 22 | service DynamicPluginService { 23 | rpc Update (DynamicPluginUpdateRequest) returns (DynamicPluginUpdateResponse) {} 24 | } 25 | 26 | message DynamicPluginUpdateRequest { 27 | string sdid = 1; 28 | string autowireType = 2; 29 | bytes pluginFile = 3; 30 | string pluginName = 4; 31 | } 32 | 33 | message DynamicPluginUpdateResponse{ 34 | bool success = 1; 35 | string message = 2; 36 | } 37 | -------------------------------------------------------------------------------- /extension/aop/dynamic_plugin/cli/dynaimc_plugin.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package cli 17 | 18 | import ( 19 | "github.com/spf13/cobra" 20 | 21 | "github.com/alibaba/ioc-golang/iocli/root" 22 | ) 23 | 24 | var pluginCommand = &cobra.Command{ 25 | Use: "goplugin", 26 | Short: "Manage go plugins", 27 | Long: "Manage go plugins", 28 | } 29 | 30 | func init() { 31 | root.Cmd.AddCommand(pluginCommand) 32 | } 33 | -------------------------------------------------------------------------------- /extension/aop/dynamic_plugin/zz_generated.ioc.go: -------------------------------------------------------------------------------- 1 | //go:build !ignore_autogenerated 2 | // +build !ignore_autogenerated 3 | 4 | // Code generated by iocli, run 'iocli gen' to re-generate 5 | 6 | package dynamic_plugin 7 | 8 | import ( 9 | "github.com/alibaba/ioc-golang/autowire" 10 | "github.com/alibaba/ioc-golang/autowire/singleton" 11 | util "github.com/alibaba/ioc-golang/autowire/util" 12 | ) 13 | 14 | func init() { 15 | dynamicPluginServiceImplStructDescriptor := &autowire.StructDescriptor{ 16 | Factory: func() interface{} { 17 | return &dynamicPluginServiceImpl{} 18 | }, 19 | Metadata: map[string]interface{}{ 20 | "aop": map[string]interface{}{}, 21 | "autowire": map[string]interface{}{}, 22 | }, 23 | DisableProxy: true, 24 | } 25 | singleton.RegisterStructDescriptor(dynamicPluginServiceImplStructDescriptor) 26 | } 27 | 28 | var _dynamicPluginServiceImplSDID string 29 | 30 | func GetdynamicPluginServiceImplSingleton() (*dynamicPluginServiceImpl, error) { 31 | if _dynamicPluginServiceImplSDID == "" { 32 | _dynamicPluginServiceImplSDID = util.GetSDIDByStructPtr(new(dynamicPluginServiceImpl)) 33 | } 34 | i, err := singleton.GetImpl(_dynamicPluginServiceImplSDID, nil) 35 | if err != nil { 36 | return nil, err 37 | } 38 | impl := i.(*dynamicPluginServiceImpl) 39 | return impl, nil 40 | } 41 | -------------------------------------------------------------------------------- /extension/aop/list/aop.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package list 17 | 18 | import ( 19 | "google.golang.org/grpc" 20 | 21 | "github.com/alibaba/ioc-golang/aop/common" 22 | 23 | "github.com/alibaba/ioc-golang/aop" 24 | "github.com/alibaba/ioc-golang/extension/aop/list/api/ioc_golang/aop/list" 25 | ) 26 | 27 | func init() { 28 | aop.RegisterAOP(aop.AOP{ 29 | Name: "list", 30 | GRPCServiceRegister: func(server *grpc.Server) { 31 | listServiceImplSingleton, _ := GetlistServiceImplSingleton(nil) 32 | list.RegisterListServiceServer(server, listServiceImplSingleton) 33 | }, 34 | ConfigLoader: func(aopConfig *common.Config) { 35 | _, _ = GetlistServiceImplSingleton(&listServiceImplParam{ 36 | AllInterfaceMetadataMap: aop.GetAllInterfaceMetadata(), 37 | AppName: aopConfig.AppName, 38 | }) 39 | }, 40 | }) 41 | } 42 | -------------------------------------------------------------------------------- /extension/aop/list/api/ioc_golang/aop/list/list.proto: -------------------------------------------------------------------------------- 1 | // EDIT IT, change to your package, service and message 2 | syntax = "proto3"; 3 | /* 4 | * Copyright (c) 2022, Alibaba Group; 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package ioc_golang.aop.list; 19 | 20 | option go_package = "ioc_golang/aop/list"; 21 | import "google/protobuf/empty.proto"; 22 | 23 | service ListService { 24 | rpc List (google.protobuf.Empty) returns (ListServiceResponse) {} 25 | } 26 | 27 | message ListServiceResponse{ 28 | repeated ServiceMetadata serviceMetadata = 1; 29 | string appName = 2; 30 | } 31 | 32 | message ServiceMetadata{ 33 | string interfaceName = 1; 34 | string implementationName = 2; 35 | repeated string methods = 3; 36 | } 37 | -------------------------------------------------------------------------------- /extension/aop/list/sorter.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package list 17 | 18 | import ( 19 | "github.com/alibaba/ioc-golang/extension/aop/list/api/ioc_golang/aop/list" 20 | ) 21 | 22 | type metadataSorter []*list.ServiceMetadata 23 | 24 | func (m metadataSorter) Len() int { 25 | return len(m) 26 | } 27 | 28 | func (m metadataSorter) Less(i, j int) bool { 29 | return m[i].InterfaceName+m[i].ImplementationName < m[j].InterfaceName+m[j].ImplementationName 30 | } 31 | 32 | func (m metadataSorter) Swap(i, j int) { 33 | m[i], m[j] = m[j], m[i] 34 | } 35 | 36 | type methodSorter []string 37 | 38 | func (m methodSorter) Len() int { 39 | return len(m) 40 | } 41 | 42 | func (m methodSorter) Less(i, j int) bool { 43 | return m[i] < m[j] 44 | } 45 | 46 | func (m methodSorter) Swap(i, j int) { 47 | m[i], m[j] = m[j], m[i] 48 | } 49 | -------------------------------------------------------------------------------- /extension/aop/list/zz_generated.ioc.go: -------------------------------------------------------------------------------- 1 | //go:build !ignore_autogenerated 2 | // +build !ignore_autogenerated 3 | 4 | // Code generated by iocli, run 'iocli gen' to re-generate 5 | 6 | package list 7 | 8 | import ( 9 | autowire "github.com/alibaba/ioc-golang/autowire" 10 | singleton "github.com/alibaba/ioc-golang/autowire/singleton" 11 | util "github.com/alibaba/ioc-golang/autowire/util" 12 | ) 13 | 14 | func init() { 15 | listServiceImplStructDescriptor := &autowire.StructDescriptor{ 16 | Factory: func() interface{} { 17 | return &listServiceImpl{} 18 | }, 19 | ParamFactory: func() interface{} { 20 | var _ listServiceImplParamInterface = &listServiceImplParam{} 21 | return &listServiceImplParam{} 22 | }, 23 | ConstructFunc: func(i interface{}, p interface{}) (interface{}, error) { 24 | param := p.(listServiceImplParamInterface) 25 | impl := i.(*listServiceImpl) 26 | return param.Init(impl) 27 | }, 28 | Metadata: map[string]interface{}{ 29 | "aop": map[string]interface{}{}, 30 | "autowire": map[string]interface{}{}, 31 | }, 32 | DisableProxy: true, 33 | } 34 | singleton.RegisterStructDescriptor(listServiceImplStructDescriptor) 35 | } 36 | 37 | type listServiceImplParamInterface interface { 38 | Init(impl *listServiceImpl) (*listServiceImpl, error) 39 | } 40 | 41 | var _listServiceImplSDID string 42 | 43 | func GetlistServiceImplSingleton(p *listServiceImplParam) (*listServiceImpl, error) { 44 | if _listServiceImplSDID == "" { 45 | _listServiceImplSDID = util.GetSDIDByStructPtr(new(listServiceImpl)) 46 | } 47 | i, err := singleton.GetImpl(_listServiceImplSDID, p) 48 | if err != nil { 49 | return nil, err 50 | } 51 | impl := i.(*listServiceImpl) 52 | return impl, nil 53 | } 54 | -------------------------------------------------------------------------------- /extension/aop/log/api/ioc_golang/aop/log/log.proto: -------------------------------------------------------------------------------- 1 | // EDIT IT, change to your package, service and message 2 | syntax = "proto3"; 3 | /* 4 | * Copyright (c) 2022, Alibaba Group; 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package ioc_golang.aop.log; 19 | 20 | option go_package = "ioc_golang/aop/log"; 21 | 22 | service LogService { 23 | rpc Log (LogRequest) returns (stream LogResponse) {} 24 | } 25 | 26 | message LogRequest{ 27 | string autowireType = 1; 28 | string sdid = 2; 29 | string methodName = 3; 30 | repeated Matcher machers = 4; 31 | bool invocation = 5; 32 | int64 level = 6; 33 | } 34 | 35 | message LogResponse{ 36 | string content = 1; 37 | } 38 | 39 | message Matcher{ 40 | int64 index = 1; 41 | string matchPath = 2; 42 | string matchValue = 3; 43 | } -------------------------------------------------------------------------------- /extension/aop/log/context.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package call 17 | 18 | import ( 19 | "github.com/sirupsen/logrus" 20 | 21 | "github.com/alibaba/ioc-golang/aop/common" 22 | logPB "github.com/alibaba/ioc-golang/extension/aop/log/api/ioc_golang/aop/log" 23 | ) 24 | 25 | // +ioc:autowire=true 26 | // +ioc:autowire:type=normal 27 | // +ioc:autowire:paramType=debugLogContextParam 28 | // +ioc:autowire:constructFunc=init 29 | // +ioc:autowire:proxy=false 30 | 31 | type debugLogContext struct { 32 | debugLogContextParam 33 | } 34 | 35 | type debugLogContextParam struct { 36 | sdid string 37 | methodName string 38 | ch chan *logPB.LogResponse 39 | fieldMatcher *common.FieldMatcher 40 | level logrus.Level 41 | invocationCtxEnable bool 42 | } 43 | 44 | func (p *debugLogContextParam) init(c *debugLogContext) (*debugLogContext, error) { 45 | c.debugLogContextParam = *p 46 | return c, nil 47 | } 48 | -------------------------------------------------------------------------------- /extension/aop/log/invocation_ctx_logs_generator.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package call 17 | 18 | import ( 19 | "fmt" 20 | "strings" 21 | ) 22 | 23 | // +ioc:autowire=true 24 | // +ioc:autowire:type=singleton 25 | // +ioc:autowire:proxy=false 26 | 27 | type InvocationCtxLogsGenerator struct { 28 | } 29 | 30 | func (i *InvocationCtxLogsGenerator) GetParamsLogs(dumpedParams []string, isCall bool) string { 31 | result := "" 32 | paramOrResponse := "Param" 33 | if !isCall { 34 | paramOrResponse = "Response" 35 | } 36 | 37 | for idx, p := range dumpedParams { 38 | result += fmt.Sprintf("%s %d: %s\n\n", paramOrResponse, idx+1, p) 39 | } 40 | return strings.TrimSuffix(result, "\n\n") 41 | } 42 | 43 | func (i *InvocationCtxLogsGenerator) GetFunctionSignatureLogs(sdid, methodName string, isCall bool) string { 44 | onToPrint := "Call" 45 | if !isCall { 46 | onToPrint = "Response" 47 | } 48 | return fmt.Sprintf("========== On %s ==========\n%s.%s()", onToPrint, sdid, methodName) 49 | } 50 | -------------------------------------------------------------------------------- /extension/aop/log/test/dynamic_change_log_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | "time" 7 | 8 | "github.com/sirupsen/logrus" 9 | "github.com/stretchr/testify/assert" 10 | 11 | "github.com/alibaba/ioc-golang" 12 | "github.com/alibaba/ioc-golang/test/iocli_command" 13 | ) 14 | 15 | func TestABCSetLogLevel(t *testing.T) { 16 | assert.Equal(t, logrus.InfoLevel, logrus.GetLevel()) 17 | assert.Nil(t, ioc.Load()) 18 | 19 | // set log level to error 20 | resull, err := iocli_command.Run([]string{"call", "singleton", "github.com/alibaba/ioc-golang/extension/aop/log.GlobalLogrusIOCCtxHook", "SetLogLevel", "--params", "[2]"}, time.Second) 21 | fmt.Println(resull) 22 | assert.Nil(t, err) 23 | assert.Equal(t, logrus.ErrorLevel, logrus.GetLevel()) 24 | 25 | // set log level to debug 26 | _, err = iocli_command.Run([]string{"call", "singleton", "github.com/alibaba/ioc-golang/extension/aop/log.GlobalLogrusIOCCtxHook", "SetLogLevel", "--params", "[5]"}, time.Second) 27 | assert.Nil(t, err) 28 | assert.Equal(t, logrus.DebugLevel, logrus.GetLevel()) 29 | } 30 | -------------------------------------------------------------------------------- /extension/aop/monitor/aop.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package monitor 17 | 18 | import ( 19 | "google.golang.org/grpc" 20 | 21 | "github.com/alibaba/ioc-golang/aop" 22 | monitorPB "github.com/alibaba/ioc-golang/extension/aop/monitor/api/ioc_golang/aop/monitor" 23 | ) 24 | 25 | const Name = "monitor" 26 | 27 | func init() { 28 | aop.RegisterAOP(aop.AOP{ 29 | Name: Name, 30 | InterceptorFactory: func() aop.Interceptor { 31 | monitorInterceptorImpl, _ := GetinterceptorImplSingleton() 32 | return monitorInterceptorImpl 33 | }, 34 | GRPCServiceRegister: func(server *grpc.Server) { 35 | monitorServiceImpl, _ := GetmonitorServiceSingleton() 36 | monitorPB.RegisterMonitorServiceServer(server, monitorServiceImpl) 37 | }, 38 | }) 39 | } 40 | -------------------------------------------------------------------------------- /extension/aop/monitor/api/ioc_golang/aop/monitor/monitor.proto: -------------------------------------------------------------------------------- 1 | // EDIT IT, change to your package, service and message 2 | syntax = "proto3"; 3 | /* 4 | * Copyright (c) 2022, Alibaba Group; 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package ioc_golang.aop.monitor; 19 | 20 | option go_package = "ioc_golang/aop/monitor"; 21 | 22 | service MonitorService { 23 | rpc Monitor (MonitorRequest) returns (stream MonitorResponse) {} 24 | } 25 | 26 | 27 | message MonitorRequest{ 28 | string sdid = 1; 29 | string method = 2; 30 | int64 interval = 3; 31 | } 32 | 33 | message MonitorResponse { 34 | repeated MonitorResponseItem monitorResponseItems = 1; 35 | } 36 | 37 | message MonitorResponseItem{ 38 | int64 timestamp = 1; 39 | string sdid = 2; 40 | string method = 3; 41 | int64 total = 4; 42 | int64 success = 5; 43 | int64 fail = 6; 44 | float avgRT = 7; 45 | float failRate = 8; 46 | } 47 | -------------------------------------------------------------------------------- /extension/aop/monitor/interceptor.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package monitor 17 | 18 | import ( 19 | "github.com/alibaba/ioc-golang/aop" 20 | ) 21 | 22 | // +ioc:autowire=true 23 | // +ioc:autowire:type=singleton 24 | // +ioc:autowire:proxy:autoInjection=false 25 | 26 | type interceptorImpl struct { 27 | monitorContext contextIOCInterface 28 | } 29 | 30 | func (w *interceptorImpl) BeforeInvoke(ctx *aop.InvocationContext) { 31 | if w.monitorContext != nil { 32 | w.monitorContext.BeforeInvoke(ctx) 33 | } 34 | } 35 | 36 | func (w *interceptorImpl) AfterInvoke(ctx *aop.InvocationContext) { 37 | if w.monitorContext != nil { 38 | w.monitorContext.AfterInvoke(ctx) 39 | } 40 | } 41 | 42 | func (w *interceptorImpl) Monitor(monitorCtx contextIOCInterface) { 43 | w.monitorContext = monitorCtx 44 | } 45 | 46 | func (w *interceptorImpl) StopMonitor() { 47 | w.monitorContext.Destroy() 48 | w.monitorContext = nil 49 | } 50 | -------------------------------------------------------------------------------- /extension/aop/monitor/interceptor_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by mockery v2.12.2. DO NOT EDIT. 2 | 3 | package monitor 4 | 5 | import ( 6 | mock "github.com/stretchr/testify/mock" 7 | 8 | aop "github.com/alibaba/ioc-golang/aop" 9 | 10 | testing "testing" 11 | ) 12 | 13 | // mockInterceptorImplIOCInterface is an autogenerated mock type for the interceptorImplIOCInterface type 14 | type mockInterceptorImplIOCInterface struct { 15 | mock.Mock 16 | } 17 | 18 | // AfterInvoke provides a mock function with given fields: ctx 19 | func (_m *mockInterceptorImplIOCInterface) AfterInvoke(ctx *aop.InvocationContext) { 20 | _m.Called(ctx) 21 | } 22 | 23 | // BeforeInvoke provides a mock function with given fields: ctx 24 | func (_m *mockInterceptorImplIOCInterface) BeforeInvoke(ctx *aop.InvocationContext) { 25 | _m.Called(ctx) 26 | } 27 | 28 | // Monitor provides a mock function with given fields: monitorCtx 29 | func (_m *mockInterceptorImplIOCInterface) Monitor(monitorCtx contextIOCInterface) { 30 | _m.Called(monitorCtx) 31 | } 32 | 33 | // StopMonitor provides a mock function with given fields: 34 | func (_m *mockInterceptorImplIOCInterface) StopMonitor() { 35 | _m.Called() 36 | } 37 | 38 | // newMockInterceptorImplIOCInterface creates a new instance of mockInterceptorImplIOCInterface. It also registers the testing.TB interface on the mock and a cleanup function to assert the mocks expectations. 39 | func newMockInterceptorImplIOCInterface(t testing.TB) *mockInterceptorImplIOCInterface { 40 | mock := &mockInterceptorImplIOCInterface{} 41 | mock.Mock.Test(t) 42 | 43 | t.Cleanup(func() { mock.AssertExpectations(t) }) 44 | 45 | return mock 46 | } 47 | -------------------------------------------------------------------------------- /extension/aop/monitor/sorter.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package monitor 17 | 18 | import ( 19 | "github.com/alibaba/ioc-golang/extension/aop/monitor/api/ioc_golang/aop/monitor" 20 | ) 21 | 22 | type monitorResponseItemsSorter []*monitor.MonitorResponseItem 23 | 24 | func (m monitorResponseItemsSorter) Len() int { 25 | return len(m) 26 | } 27 | 28 | func (m monitorResponseItemsSorter) Less(i, j int) bool { 29 | return m[i].Sdid+m[i].Method < m[j].Sdid+m[j].Method 30 | } 31 | 32 | func (m monitorResponseItemsSorter) Swap(i, j int) { 33 | m[i], m[j] = m[j], m[i] 34 | } 35 | -------------------------------------------------------------------------------- /extension/aop/monitor/util.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package monitor 17 | 18 | func getAverageInt64(input []int64) float32 { 19 | length := len(input) 20 | if length == 0 { 21 | return 0 22 | } 23 | sum := int64(0) 24 | for _, v := range input { 25 | sum += v 26 | } 27 | return float32(sum) / float32(length) 28 | } 29 | -------------------------------------------------------------------------------- /extension/aop/trace/api/ioc_golang/aop/trace/trace.proto: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | syntax = "proto3"; 17 | 18 | package ioc_golang.aop.trace; 19 | 20 | import "extension/aop/trace/api/jaeger/jaeger_model.proto"; 21 | 22 | option go_package = "ioc_golang/aop/trace"; 23 | 24 | service TraceService { 25 | rpc Trace (TraceRequest) returns (stream TraceResponse) {} 26 | } 27 | 28 | message TraceRequest{ 29 | string sdid = 1; 30 | string method = 2; 31 | repeated Matcher matchers = 3; 32 | string pushToCollectorAddress = 4; 33 | int64 maxDepth = 5; 34 | int64 maxLength = 6; 35 | } 36 | 37 | message Matcher{ 38 | int64 index = 1; 39 | string matchPath = 2; 40 | string matchValue = 3; 41 | } 42 | 43 | message TraceResponse{ 44 | string collectorAddress = 1; 45 | bytes thriftSerializedSpans = 2; 46 | repeated jaeger.api_v2.Trace traces = 3; 47 | } 48 | -------------------------------------------------------------------------------- /extension/aop/trace/common/jaeger_endpoint.go: -------------------------------------------------------------------------------- 1 | package common 2 | 3 | import "fmt" 4 | 5 | func GetJaegerCollectorEndpoint(jaegerCollectorAddress string) string { 6 | return fmt.Sprintf("http://%s/api/traces?format=jaeger.thrift", jaegerCollectorAddress) 7 | } 8 | -------------------------------------------------------------------------------- /extension/aop/trace/common/span_key.go: -------------------------------------------------------------------------------- 1 | package common 2 | 3 | const ( 4 | SpanParamsKey = "params" 5 | SpanReturnValuesKey = "returnValues" 6 | DefaultRecordValuesDepth = 5 7 | DefaultRecordValuesLength = 1000 8 | ) 9 | -------------------------------------------------------------------------------- /extension/aop/trace/config.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package trace 17 | 18 | type TraceConfig struct { 19 | CollectorAddress string `yaml:"collector-address"` 20 | ValueDepth int `yaml:"value-depth"` 21 | } 22 | -------------------------------------------------------------------------------- /extension/aop/trace/log/interceptor.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package log 17 | 18 | import ( 19 | "github.com/opentracing/opentracing-go" 20 | 21 | "github.com/alibaba/ioc-golang/autowire/util" 22 | "github.com/alibaba/ioc-golang/extension/autowire/allimpls" 23 | 24 | "github.com/alibaba/ioc-golang/logger" 25 | ) 26 | 27 | type CurrentSpanGetter func() opentracing.Span 28 | 29 | type Writer interface { 30 | Write(p []byte) 31 | } 32 | 33 | type TraceExtensionWriter interface { 34 | SetTraceLoggerWriter(writer Writer) 35 | Name() string 36 | } 37 | 38 | func RunRegisteredTraceLoggerWriterFunc(getter CurrentSpanGetter) { 39 | allTraceExtensionWriterImpls, err := allimpls.GetImpl(util.GetSDIDByStructPtr(new(TraceExtensionWriter))) 40 | if err != nil { 41 | logger.Red("[AOP Trace] Get all trace logger writer failed with error = %s", err) 42 | return 43 | } 44 | for _, impl := range allTraceExtensionWriterImpls.([]TraceExtensionWriter) { 45 | impl.SetTraceLoggerWriter(newTraceLoggerWriter(getter, impl.Name())) 46 | logger.Blue("[AOP] [Trace] Set trace logger to %s success", impl.Name()) 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /extension/aop/trace/log/wrapperWriter.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package log 17 | 18 | import ( 19 | log2 "github.com/opentracing/opentracing-go/log" 20 | ) 21 | 22 | type TraceLoggerWriter struct { 23 | spanGetter CurrentSpanGetter 24 | logKey string 25 | } 26 | 27 | func (w *TraceLoggerWriter) Write(p []byte) { 28 | currentSpan := w.spanGetter() 29 | if currentSpan != nil { 30 | // only when tracing, get span and write log 31 | currentSpan.LogFields(log2.String(w.logKey, string(p))) 32 | } 33 | } 34 | 35 | func newTraceLoggerWriter(spanGetter CurrentSpanGetter, logKey string) Writer { 36 | return &TraceLoggerWriter{ 37 | spanGetter: spanGetter, 38 | logKey: logKey, 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /extension/aop/trace/sorter.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package trace 17 | 18 | import ( 19 | "github.com/jaegertracing/jaeger/model" 20 | ) 21 | 22 | type traceSorter []*model.Trace 23 | 24 | func (m traceSorter) Len() int { 25 | return len(m) 26 | } 27 | 28 | func (m traceSorter) Less(i, j int) bool { 29 | if m[i].Spans == nil || len(m[i].Spans) == 0 || m[j].Spans == nil || len(m[j].Spans) == 0 { 30 | return true 31 | } 32 | return m[i].Spans[0].StartTime.Before(m[j].Spans[0].StartTime) 33 | } 34 | 35 | func (m traceSorter) Swap(i, j int) { 36 | m[i], m[j] = m[j], m[i] 37 | } 38 | -------------------------------------------------------------------------------- /extension/aop/trace/sorter_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package trace 17 | 18 | import ( 19 | "sort" 20 | "testing" 21 | "time" 22 | 23 | "github.com/jaegertracing/jaeger/model" 24 | "github.com/stretchr/testify/assert" 25 | ) 26 | 27 | func TestMonitorResponseItemsSorter(t *testing.T) { 28 | traceItems := make(traceSorter, 0) 29 | now := time.Now() 30 | traceItems = append(traceItems, &model.Trace{ 31 | Spans: []*model.Span{ 32 | { 33 | StartTime: now.Add(time.Millisecond), 34 | }, 35 | }, 36 | }) 37 | traceItems = append(traceItems, &model.Trace{ 38 | Spans: []*model.Span{ 39 | { 40 | StartTime: now.Add(time.Millisecond * 2), 41 | }, 42 | }, 43 | }) 44 | 45 | traceItems = append(traceItems, &model.Trace{ 46 | Spans: []*model.Span{ 47 | { 48 | StartTime: now, 49 | }, 50 | }, 51 | }) 52 | 53 | sort.Sort(traceItems) 54 | assert.Equal(t, now.Unix(), traceItems[0].Spans[0].StartTime.Unix()) 55 | assert.Equal(t, now.Add(time.Millisecond).Unix(), traceItems[1].Spans[0].StartTime.Unix()) 56 | assert.Equal(t, now.Add(time.Millisecond*2).Unix(), traceItems[2].Spans[0].StartTime.Unix()) 57 | } 58 | -------------------------------------------------------------------------------- /extension/aop/trace/span_with_parent.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package trace 17 | 18 | import ( 19 | "github.com/opentracing/opentracing-go" 20 | ) 21 | 22 | type spanWithParent struct { 23 | span opentracing.Span 24 | parentSpan *spanWithParent 25 | } 26 | 27 | func newSpanWithParent(span opentracing.Span, parentSpan *spanWithParent) *spanWithParent { 28 | return &spanWithParent{ 29 | span: span, 30 | parentSpan: parentSpan, 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /extension/aop/transaction/aop.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package transaction 17 | 18 | import "github.com/alibaba/ioc-golang/aop" 19 | 20 | const Name = "transaction" 21 | 22 | func init() { 23 | aop.RegisterAOP(aop.AOP{ 24 | Name: Name, 25 | InterceptorFactory: func() aop.Interceptor { 26 | interfaceImpl, _ := GetinterceptorImplSingleton() 27 | return interfaceImpl 28 | }, 29 | }) 30 | } 31 | -------------------------------------------------------------------------------- /extension/aop/transaction/cli/gen.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package cli 17 | 18 | import ( 19 | "sigs.k8s.io/controller-tools/pkg/markers" 20 | ) 21 | 22 | const transactionFunctionAnnotation = "ioc:tx:func" 23 | 24 | // +ioc:autowire=true 25 | // +ioc:autowire:type=allimpls 26 | // +ioc:autowire:proxy=false 27 | // +ioc:autowire:implements=github.com/alibaba/ioc-golang/iocli/gen/marker.DefinitionGetter 28 | 29 | type transactionFunctionMarker struct { 30 | } 31 | 32 | func (m *transactionFunctionMarker) GetMarkerDefinition() *markers.Definition { 33 | return markers.Must(markers.MakeDefinition(transactionFunctionAnnotation, markers.DescribesType, "")) 34 | } 35 | -------------------------------------------------------------------------------- /extension/aop/transaction/context_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by mockery v2.12.2. DO NOT EDIT. 2 | 3 | package transaction 4 | 5 | import ( 6 | mock "github.com/stretchr/testify/mock" 7 | 8 | aop "github.com/alibaba/ioc-golang/aop" 9 | 10 | testing "testing" 11 | ) 12 | 13 | // mockContextIOCInterface is an autogenerated mock type for the contextIOCInterface type 14 | type mockContextIOCInterface struct { 15 | mock.Mock 16 | } 17 | 18 | // AddSuccessfullyCalledInvocationCtx provides a mock function with given fields: ctx 19 | func (_m *mockContextIOCInterface) AddSuccessfullyCalledInvocationCtx(ctx *aop.InvocationContext) { 20 | _m.Called(ctx) 21 | } 22 | 23 | // Failed provides a mock function with given fields: err 24 | func (_m *mockContextIOCInterface) Failed(err error) { 25 | _m.Called(err) 26 | } 27 | 28 | // Finish provides a mock function with given fields: 29 | func (_m *mockContextIOCInterface) Finish() { 30 | _m.Called() 31 | } 32 | 33 | // GetEntranceMethodFullName provides a mock function with given fields: 34 | func (_m *mockContextIOCInterface) GetEntranceMethodFullName() string { 35 | ret := _m.Called() 36 | 37 | var r0 string 38 | if rf, ok := ret.Get(0).(func() string); ok { 39 | r0 = rf() 40 | } else { 41 | r0 = ret.Get(0).(string) 42 | } 43 | 44 | return r0 45 | } 46 | 47 | // newMockContextIOCInterface creates a new instance of mockContextIOCInterface. It also registers the testing.TB interface on the mock and a cleanup function to assert the mocks expectations. 48 | func newMockContextIOCInterface(t testing.TB) *mockContextIOCInterface { 49 | mock := &mockContextIOCInterface{} 50 | mock.Mock.Test(t) 51 | 52 | t.Cleanup(func() { mock.AssertExpectations(t) }) 53 | 54 | return mock 55 | } 56 | -------------------------------------------------------------------------------- /extension/aop/transaction/metadata.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package transaction 17 | 18 | import ( 19 | "github.com/alibaba/ioc-golang/aop" 20 | "github.com/alibaba/ioc-golang/autowire" 21 | ) 22 | 23 | func parseRollbackMethodNameFromSDMetadata(metadata autowire.Metadata, methodName string) (string, bool) { 24 | if aopMetadata := aop.ParseAOPMetadataFromSDMetadata(metadata); aopMetadata != nil { 25 | if txAOPMetadataVal, ok := aopMetadata[Name]; ok { 26 | if txAOPMetadata, ok2 := txAOPMetadataVal.(map[string]string); ok2 { 27 | rollbackMethodName, found := txAOPMetadata[methodName] 28 | return rollbackMethodName, found 29 | } 30 | } 31 | } 32 | return "", false 33 | } 34 | -------------------------------------------------------------------------------- /extension/aop/watch/aop.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package watch 17 | 18 | import ( 19 | "google.golang.org/grpc" 20 | 21 | "github.com/alibaba/ioc-golang/aop" 22 | watchPB "github.com/alibaba/ioc-golang/extension/aop/watch/api/ioc_golang/aop/watch" 23 | ) 24 | 25 | const Name = "watch" 26 | 27 | func init() { 28 | aop.RegisterAOP(aop.AOP{ 29 | Name: Name, 30 | InterceptorFactory: func() aop.Interceptor { 31 | impl, _ := GetinterceptorImplIOCInterfaceSingleton() 32 | return impl 33 | }, 34 | GRPCServiceRegister: func(server *grpc.Server) { 35 | watchInterface, _ := GetwatchServiceSingleton() 36 | watchPB.RegisterWatchServiceServer(server, watchInterface) 37 | }, 38 | }) 39 | } 40 | -------------------------------------------------------------------------------- /extension/aop/watch/api/ioc_golang/aop/watch/watch.proto: -------------------------------------------------------------------------------- 1 | // EDIT IT, change to your package, service and message 2 | syntax = "proto3"; 3 | /* 4 | * Copyright (c) 2022, Alibaba Group; 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package ioc_golang.aop.watch; 19 | 20 | option go_package = "ioc_golang/aop/watch"; 21 | 22 | service WatchService { 23 | rpc Watch (WatchRequest) returns (stream WatchResponse) {} 24 | } 25 | 26 | 27 | message WatchRequest{ 28 | string sdid = 1; 29 | string method = 2; 30 | int64 maxDepth = 3; 31 | int64 maxLength = 4; 32 | repeated Matcher matchers = 5; 33 | } 34 | 35 | message Matcher{ 36 | int64 index = 1; 37 | string matchPath = 2; 38 | string matchValue = 3; 39 | } 40 | 41 | message WatchResponse{ 42 | string sdid = 1; 43 | string methodName = 2; 44 | repeated string params = 3; 45 | repeated string returnValues = 4; 46 | } 47 | -------------------------------------------------------------------------------- /extension/autowire/allimpls/allimpls_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package allimpls 17 | 18 | import ( 19 | "testing" 20 | 21 | "github.com/alibaba/ioc-golang/autowire/util" 22 | 23 | "github.com/stretchr/testify/assert" 24 | 25 | "github.com/alibaba/ioc-golang/autowire" 26 | ) 27 | 28 | type mockImpl struct { 29 | } 30 | 31 | func TestAutowire_RegisterAndGetAllStructDescriptors(t *testing.T) { 32 | t.Run("test config autowire register and get all struct descriptors", func(t *testing.T) { 33 | sd := &autowire.StructDescriptor{ 34 | Factory: func() interface{} { 35 | return &mockImpl{} 36 | }, 37 | } 38 | RegisterStructDescriptor(sd) 39 | a := &Autowire{} 40 | allStructDesc := a.GetAllStructDescriptors() 41 | assert.NotNil(t, allStructDesc) 42 | structDesc, ok := allStructDesc[util.GetSDIDByStructPtr(&mockImpl{})] 43 | assert.True(t, ok) 44 | assert.Equal(t, util.GetSDIDByStructPtr(&mockImpl{}), structDesc.ID()) 45 | }) 46 | } 47 | 48 | func TestAutowire_TagKey(t *testing.T) { 49 | t.Run("test allimpls autowire tag", func(t *testing.T) { 50 | a := &Autowire{} 51 | assert.Equal(t, Name, a.TagKey()) 52 | }) 53 | } 54 | -------------------------------------------------------------------------------- /extension/autowire/allimpls/cli/marker.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package cli 17 | 18 | import "sigs.k8s.io/controller-tools/pkg/markers" 19 | 20 | // +ioc:autowire=true 21 | // +ioc:autowire:type=allimpls 22 | // +ioc:autowire:proxy=false 23 | // +ioc:autowire:implements=github.com/alibaba/ioc-golang/iocli/gen/marker.DefinitionGetter 24 | 25 | type iocGolangAutowireAllImplsTypeMarker struct { 26 | } 27 | 28 | func (m *iocGolangAutowireAllImplsTypeMarker) GetMarkerDefinition() *markers.Definition { 29 | return markers.Must(markers.MakeDefinition(allimplsAutowireTypeAnnotation, markers.DescribesType, "")) 30 | } 31 | -------------------------------------------------------------------------------- /extension/autowire/common/util.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package common 17 | 18 | import ( 19 | "strings" 20 | "unicode" 21 | "unicode/utf8" 22 | ) 23 | 24 | func ParseInterfacePkgAndInterfaceName(interfaceID string) (string, string) { 25 | splited := strings.Split(interfaceID, ".") 26 | if len(splited) == 1 { 27 | return "", splited[0] 28 | } 29 | return strings.Join(splited[:len(splited)-1], "."), splited[len(splited)-1] 30 | } 31 | 32 | //IsExportedMethod Is this an exported - upper case - name 33 | func IsExportedMethod(name string) bool { 34 | s, _ := utf8.DecodeRuneInString(name) 35 | return unicode.IsUpper(s) 36 | } 37 | -------------------------------------------------------------------------------- /extension/autowire/config/config_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package config 17 | 18 | import ( 19 | "testing" 20 | 21 | "github.com/stretchr/testify/assert" 22 | 23 | "github.com/alibaba/ioc-golang/autowire" 24 | ) 25 | 26 | type mockImpl struct { 27 | } 28 | 29 | const mockImplName = "github.com/alibaba/ioc-golang/extension/autowire/config.mockImpl" 30 | 31 | func TestAutowire_RegisterAndGetAllStructDescriptors(t *testing.T) { 32 | t.Run("test config autowire register and get all struct descriptors", func(t *testing.T) { 33 | sd := &autowire.StructDescriptor{ 34 | Factory: func() interface{} { 35 | return &mockImpl{} 36 | }, 37 | } 38 | RegisterStructDescriptor(sd) 39 | a := &Autowire{} 40 | allStructDesc := a.GetAllStructDescriptors() 41 | assert.NotNil(t, allStructDesc) 42 | structDesc, ok := allStructDesc[mockImplName] 43 | assert.True(t, ok) 44 | assert.Equal(t, mockImplName, structDesc.ID()) 45 | }) 46 | } 47 | 48 | func TestAutowire_TagKey(t *testing.T) { 49 | t.Run("test config autowire tag", func(t *testing.T) { 50 | a := &Autowire{} 51 | assert.Equal(t, Name, a.TagKey()) 52 | }) 53 | } 54 | -------------------------------------------------------------------------------- /extension/autowire/grpc/grpc_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package grpc 17 | 18 | import ( 19 | "testing" 20 | 21 | "github.com/stretchr/testify/assert" 22 | 23 | "github.com/alibaba/ioc-golang/autowire" 24 | ) 25 | 26 | type mockImpl struct { 27 | } 28 | 29 | const mockImplName = "github.com/alibaba/ioc-golang/extension/autowire/grpc.mockImpl" 30 | 31 | func TestAutowire_RegisterAndGetAllStructDescriptors(t *testing.T) { 32 | t.Run("test config autowire register and get all struct descriptors", func(t *testing.T) { 33 | sd := &autowire.StructDescriptor{ 34 | Factory: func() interface{} { 35 | return &mockImpl{} 36 | }, 37 | } 38 | RegisterStructDescriptor(sd) 39 | a := &Autowire{} 40 | allStructDesc := a.GetAllStructDescriptors() 41 | assert.NotNil(t, allStructDesc) 42 | structDesc, ok := allStructDesc[mockImplName] 43 | assert.True(t, ok) 44 | assert.Equal(t, mockImplName, structDesc.ID()) 45 | }) 46 | } 47 | 48 | func TestAutowire_TagKey(t *testing.T) { 49 | t.Run("test grpc autowire tag", func(t *testing.T) { 50 | a := &Autowire{} 51 | assert.Equal(t, Name, a.TagKey()) 52 | }) 53 | } 54 | -------------------------------------------------------------------------------- /extension/autowire/grpc/sdid_parser.go: -------------------------------------------------------------------------------- 1 | package grpc 2 | 3 | import ( 4 | "github.com/alibaba/ioc-golang/autowire" 5 | ) 6 | 7 | type sdIDParser struct { 8 | } 9 | 10 | /* 11 | Parse support parse field like: 12 | ResourceServiceClient resources.ResourceServiceClient `grpc:"resource-service"` 13 | to struct descriptor ID 'ResourceServiceClient-ResourceServiceClient' 14 | */ 15 | func (p *sdIDParser) Parse(fi *autowire.FieldInfo) (string, error) { 16 | return fi.FieldType, nil 17 | } 18 | -------------------------------------------------------------------------------- /extension/autowire/rpc/protocol/protocol.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package protocol 17 | 18 | import "dubbo.apache.org/dubbo-go/v3/protocol" 19 | 20 | type Protocol interface { 21 | Invoke(invocation protocol.Invocation) protocol.Result 22 | Export(invoker protocol.Invoker) protocol.Exporter 23 | } 24 | -------------------------------------------------------------------------------- /extension/autowire/rpc/protocol/protocol_impl/constant.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package protocol_impl 17 | 18 | const IOCProtocolName = "ioc" 19 | const DefaultSchema = "http" 20 | -------------------------------------------------------------------------------- /extension/autowire/rpc/protocol/protocol_impl/param.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package protocol_impl 17 | 18 | type Param struct { 19 | Address string 20 | Timeout string 21 | ExportPort string 22 | } 23 | 24 | func (p *Param) Init(iocProtocol *IOCProtocol) (*IOCProtocol, error) { 25 | iocProtocol.address = p.Address 26 | iocProtocol.exportPort = p.ExportPort 27 | iocProtocol.timeout = p.Timeout 28 | return iocProtocol, nil 29 | } 30 | -------------------------------------------------------------------------------- /extension/autowire/rpc/rpc_client/param.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package rpc_client 17 | 18 | type Param struct { 19 | Address string 20 | Timeout string 21 | } 22 | -------------------------------------------------------------------------------- /extension/autowire/rpc/rpc_client/proxy_invoker.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package rpc_client 17 | 18 | import ( 19 | "context" 20 | 21 | "dubbo.apache.org/dubbo-go/v3/common" 22 | dubboProtocol "dubbo.apache.org/dubbo-go/v3/protocol" 23 | 24 | "github.com/alibaba/ioc-golang/extension/autowire/rpc/protocol" 25 | ) 26 | 27 | type proxyInvoker struct { 28 | protocol protocol.Protocol 29 | sdid string 30 | } 31 | 32 | func (i *proxyInvoker) GetURL() *common.URL { 33 | return nil 34 | } 35 | 36 | func (i *proxyInvoker) IsAvailable() bool { 37 | return true 38 | } 39 | 40 | func (i *proxyInvoker) Destroy() { 41 | 42 | } 43 | 44 | func (i *proxyInvoker) Invoke(ctx context.Context, invocation dubboProtocol.Invocation) dubboProtocol.Result { 45 | invocation.SetAttachment("sdid", i.sdid) 46 | return i.protocol.Invoke(invocation) 47 | } 48 | 49 | func newProxyInvoker(protocol protocol.Protocol, sdid string) dubboProtocol.Invoker { 50 | return &proxyInvoker{protocol: protocol, sdid: sdid} 51 | } 52 | -------------------------------------------------------------------------------- /extension/autowire/rpc/rpc_client/sdid_parser.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package rpc_client 17 | 18 | import ( 19 | "strings" 20 | 21 | "github.com/alibaba/ioc-golang/autowire" 22 | "github.com/alibaba/ioc-golang/autowire/util" 23 | ) 24 | 25 | type sdidParser struct { 26 | } 27 | 28 | func (s *sdidParser) Parse(fi *autowire.FieldInfo) (string, error) { 29 | injectStructName := fi.FieldType 30 | splitedTagValue := strings.Split(fi.TagValue, ",") 31 | if len(splitedTagValue) > 0 && splitedTagValue[0] != "" { 32 | injectStructName = splitedTagValue[0] 33 | } else { 34 | // no struct sdid in tag 35 | if !util.IsPointerField(fi.FieldReflectType) && strings.HasSuffix(injectStructName, "IOCRPCClient") { 36 | // is interface field without valid sdid from tag value, and with 'IOCInterface' suffix 37 | // load trim suffix as sdid 38 | var err error 39 | injectStructName, err = util.ToRPCClientStubSDID(injectStructName) 40 | if err != nil { 41 | return "", err 42 | } 43 | } 44 | } 45 | return autowire.GetSDIDByAliasIfNecessary(injectStructName), nil 46 | } 47 | -------------------------------------------------------------------------------- /extension/autowire/rpc/rpc_service/param.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package rpc_service 17 | 18 | type Param struct { 19 | ExportPort string 20 | } 21 | 22 | var defaultParam = &Param{ 23 | ExportPort: "2022", 24 | } 25 | 26 | func SetParam(param *Param) { 27 | defaultParam = param 28 | } 29 | 30 | func GetParam() *Param { 31 | return defaultParam 32 | } 33 | -------------------------------------------------------------------------------- /extension/autowire/rpc/rpc_service/param_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package rpc_service 17 | 18 | import ( 19 | "testing" 20 | 21 | "github.com/stretchr/testify/assert" 22 | ) 23 | 24 | func TestSetParam(t *testing.T) { 25 | assert.Equal(t, "2022", defaultParam.ExportPort) 26 | SetParam(&Param{ 27 | ExportPort: "2023", 28 | }) 29 | assert.Equal(t, "2023", defaultParam.ExportPort) 30 | } 31 | -------------------------------------------------------------------------------- /extension/autowire/rpc/rpc_service/rpc_service_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package rpc_service 17 | 18 | import ( 19 | "testing" 20 | 21 | "github.com/stretchr/testify/assert" 22 | 23 | "github.com/alibaba/ioc-golang/autowire" 24 | ) 25 | 26 | type mockImpl struct { 27 | } 28 | 29 | const mockImplName = "github.com/alibaba/ioc-golang/extension/autowire/rpc/rpc_service.mockImpl" 30 | 31 | func TestAutowire_RegisterAndGetAllStructDescriptors(t *testing.T) { 32 | t.Run("test config autowire register and get all struct descriptors", func(t *testing.T) { 33 | sd := &autowire.StructDescriptor{ 34 | Factory: func() interface{} { 35 | return &mockImpl{} 36 | }, 37 | } 38 | RegisterStructDescriptor(sd) 39 | a := &Autowire{} 40 | allStructDesc := a.GetAllStructDescriptors() 41 | assert.NotNil(t, allStructDesc) 42 | structDesc, ok := allStructDesc[mockImplName] 43 | assert.True(t, ok) 44 | assert.Equal(t, mockImplName, structDesc.ID()) 45 | }) 46 | } 47 | 48 | func TestAutowire_TagKey(t *testing.T) { 49 | t.Run("test rpc autowire tag", func(t *testing.T) { 50 | a := &Autowire{} 51 | assert.Equal(t, Name, a.TagKey()) 52 | }) 53 | } 54 | -------------------------------------------------------------------------------- /extension/config/config_float64_describer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package config 17 | 18 | // 19 | // Why? 20 | // 21 | // In many scenarios, float64 may be use, 22 | // such as the price of order. 23 | // 24 | 25 | // +ioc:autowire=true 26 | // +ioc:autowire:baseType=true 27 | // +ioc:autowire:type=config 28 | // +ioc:autowire:paramType=ConfigFloat64 29 | // +ioc:autowire:constructFunc=new 30 | // +ioc:autowire:proxy:autoInjection=false 31 | 32 | type ConfigFloat64 float64 33 | 34 | func (ci *ConfigFloat64) Value() float64 { 35 | return float64(*ci) 36 | } 37 | 38 | func (ci *ConfigFloat64) new(impl *ConfigFloat64) (*ConfigFloat64, error) { 39 | *impl = *ci 40 | return impl, nil 41 | } 42 | 43 | func FromFloat64(val float64) *ConfigFloat64 { 44 | configFloat64 := ConfigFloat64(val) 45 | return &configFloat64 46 | } 47 | -------------------------------------------------------------------------------- /extension/config/config_int64_describer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package config 17 | 18 | // 19 | // Why? 20 | // 21 | // In many scenarios, int64 may be use, 22 | // such as the nextId by the snowflake algorithm. 23 | // 24 | 25 | // +ioc:autowire=true 26 | // +ioc:autowire:baseType=true 27 | // +ioc:autowire:type=config 28 | // +ioc:autowire:paramType=ConfigInt64 29 | // +ioc:autowire:constructFunc=new 30 | // +ioc:autowire:proxy:autoInjection=false 31 | 32 | type ConfigInt64 int64 33 | 34 | func (ci *ConfigInt64) Value() int64 { 35 | return int64(*ci) 36 | } 37 | 38 | func (ci *ConfigInt64) new(impl *ConfigInt64) (*ConfigInt64, error) { 39 | *impl = *ci 40 | return impl, nil 41 | } 42 | 43 | func FromInt64(val int64) *ConfigInt64 { 44 | configInt64 := ConfigInt64(val) 45 | return &configInt64 46 | } 47 | -------------------------------------------------------------------------------- /extension/config/config_int_describer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package config 17 | 18 | // +ioc:autowire=true 19 | // +ioc:autowire:baseType=true 20 | // +ioc:autowire:type=config 21 | // +ioc:autowire:paramType=ConfigInt 22 | // +ioc:autowire:constructFunc=new 23 | // +ioc:autowire:proxy:autoInjection=false 24 | 25 | type ConfigInt int 26 | 27 | func (ci *ConfigInt) Value() int { 28 | return int(*ci) 29 | } 30 | 31 | func (param *ConfigInt) new(impl *ConfigInt) (*ConfigInt, error) { 32 | *impl = *param 33 | return impl, nil 34 | } 35 | 36 | func FromInt(val int) *ConfigInt { 37 | configInt := ConfigInt(val) 38 | return &configInt 39 | } 40 | -------------------------------------------------------------------------------- /extension/config/config_map_describer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package config 17 | 18 | // +ioc:autowire=true 19 | // +ioc:autowire:baseType=true 20 | // +ioc:autowire:type=config 21 | // +ioc:autowire:paramType=ConfigMap 22 | // +ioc:autowire:constructFunc=new 23 | // +ioc:autowire:proxy:autoInjection=false 24 | 25 | type ConfigMap map[string]interface{} 26 | 27 | func (ci *ConfigMap) Value() map[string]interface{} { 28 | return *ci 29 | } 30 | 31 | func (ci *ConfigMap) new(impl *ConfigMap) (*ConfigMap, error) { 32 | *impl = *ci 33 | return impl, nil 34 | } 35 | 36 | func FromMap(val map[string]interface{}) *ConfigMap { 37 | configMap := ConfigMap(val) 38 | return &configMap 39 | } 40 | -------------------------------------------------------------------------------- /extension/config/config_slice_describer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package config 17 | 18 | // +ioc:autowire=true 19 | // +ioc:autowire:baseType=true 20 | // +ioc:autowire:type=config 21 | // +ioc:autowire:paramType=ConfigSlice 22 | // +ioc:autowire:constructFunc=new 23 | // +ioc:autowire:proxy:autoInjection=false 24 | 25 | type ConfigSlice []interface{} 26 | 27 | func (cs *ConfigSlice) Value() []interface{} { 28 | return *cs 29 | } 30 | 31 | func (ci *ConfigSlice) new(impl *ConfigSlice) (*ConfigSlice, error) { 32 | *impl = *ci 33 | return impl, nil 34 | } 35 | 36 | func FromSlice(val []interface{}) *ConfigSlice { 37 | configSlice := ConfigSlice(val) 38 | return &configSlice 39 | } 40 | -------------------------------------------------------------------------------- /extension/config/config_string_describer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package config 17 | 18 | // +ioc:autowire=true 19 | // +ioc:autowire:baseType=true 20 | // +ioc:autowire:type=config 21 | // +ioc:autowire:paramType=ConfigString 22 | // +ioc:autowire:constructFunc=new 23 | // +ioc:autowire:proxy:autoInjection=false 24 | 25 | type ConfigString string 26 | 27 | func (ci *ConfigString) Value() string { 28 | return string(*ci) 29 | } 30 | 31 | func (ci *ConfigString) new(impl *ConfigString) (*ConfigString, error) { 32 | *impl = *ci 33 | return impl, nil 34 | } 35 | 36 | func FromString(val string) *ConfigString { 37 | configInt := ConfigString(val) 38 | return &configInt 39 | } 40 | -------------------------------------------------------------------------------- /extension/config_center/nacos/param.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package nacos 17 | 18 | import ( 19 | "github.com/nacos-group/nacos-sdk-go/v2/clients" 20 | "github.com/nacos-group/nacos-sdk-go/v2/vo" 21 | ) 22 | 23 | type Param struct { 24 | vo.NacosClientParam 25 | } 26 | 27 | func (c *Param) New(impl *ConfigClient) (*ConfigClient, error) { 28 | var err error 29 | impl.IConfigClient, err = clients.NewConfigClient(c.NacosClientParam) 30 | if err != nil { 31 | return nil, err 32 | } 33 | return impl, nil 34 | } 35 | -------------------------------------------------------------------------------- /extension/db/gorm/param.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package gorm 17 | 18 | import ( 19 | "gorm.io/driver/mysql" 20 | "gorm.io/gorm" 21 | ) 22 | 23 | type Param struct { 24 | Host string `yaml:"host"` 25 | Port string `yaml:"port"` 26 | Username string `yaml:"username"` 27 | Password string `yaml:"password"` 28 | DBName string `yaml:"dbname"` 29 | } 30 | 31 | func (c *Param) New(mysqlImpl *GORMDB) (*GORMDB, error) { 32 | rawDB, err := gorm.Open(mysql.Open(getMysqlLinkStr(c)), &gorm.Config{}) 33 | if err != nil { 34 | return nil, err 35 | } 36 | mysqlImpl.db = rawDB 37 | return mysqlImpl, err 38 | } 39 | 40 | func getMysqlLinkStr(conf *Param) string { 41 | return conf.Username + ":" + conf.Password + "@tcp(" + conf.Host + ":" + conf.Port + ")/" + conf.DBName + 42 | "?charset=utf8&parseTime=True&loc=Local" 43 | } 44 | -------------------------------------------------------------------------------- /extension/db/gorm/param_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package gorm 17 | 18 | import ( 19 | "fmt" 20 | "testing" 21 | 22 | "github.com/stretchr/testify/assert" 23 | ) 24 | 25 | func Test_getMysqlLinkStr(t *testing.T) { 26 | conf := &Param{ 27 | Host: "192.168.1.1", 28 | Port: "1234", 29 | Username: "admin", 30 | Password: "admin", 31 | DBName: "mydb", 32 | } 33 | got := getMysqlLinkStr(conf) 34 | assert.Equal(t, fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", conf.Username, conf.Password, conf.Host, conf.Port, conf.DBName), got) 35 | } 36 | -------------------------------------------------------------------------------- /extension/db/gorm/test/ioc_golang.yaml: -------------------------------------------------------------------------------- 1 | autowire: 2 | normal: 3 | github.com/alibaba/ioc-golang/extension/db/gorm.GORMDB: 4 | my-mysql: 5 | param: 6 | host: "192.168.1.1" 7 | port: 1234 8 | username: "admin" 9 | password: "admin" 10 | dbname: "mydb" 11 | param: 12 | host: "127.0.0.1" 13 | port: 3306 14 | username: "root" 15 | password: "root" 16 | dbname: "test" -------------------------------------------------------------------------------- /extension/imports/boot/imports.go: -------------------------------------------------------------------------------- 1 | //go:build !disableAOP 2 | 3 | /* 4 | * Copyright (c) 2022, Alibaba Group; 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package boot 19 | 20 | import ( 21 | _ "github.com/alibaba/ioc-golang/extension/aop/call" 22 | _ "github.com/alibaba/ioc-golang/extension/aop/dynamic_plugin" 23 | _ "github.com/alibaba/ioc-golang/extension/aop/list" 24 | _ "github.com/alibaba/ioc-golang/extension/aop/log" 25 | _ "github.com/alibaba/ioc-golang/extension/aop/monitor" 26 | _ "github.com/alibaba/ioc-golang/extension/aop/trace" 27 | _ "github.com/alibaba/ioc-golang/extension/aop/transaction" 28 | _ "github.com/alibaba/ioc-golang/extension/aop/watch" 29 | ) 30 | -------------------------------------------------------------------------------- /extension/imports/boot/imports_default.go: -------------------------------------------------------------------------------- 1 | //go:build disableAOP 2 | 3 | /* 4 | * Copyright (c) 2022, Alibaba Group; 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package boot 19 | -------------------------------------------------------------------------------- /extension/imports/cli/imports.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package cli 17 | 18 | import ( 19 | _ "github.com/alibaba/ioc-golang/extension/aop/call/cli" 20 | _ "github.com/alibaba/ioc-golang/extension/aop/dynamic_plugin/cli" 21 | _ "github.com/alibaba/ioc-golang/extension/aop/list/cli" 22 | _ "github.com/alibaba/ioc-golang/extension/aop/log/cli" 23 | _ "github.com/alibaba/ioc-golang/extension/aop/monitor/cli" 24 | _ "github.com/alibaba/ioc-golang/extension/aop/trace/cli" 25 | _ "github.com/alibaba/ioc-golang/extension/aop/transaction/cli" 26 | _ "github.com/alibaba/ioc-golang/extension/aop/watch/cli" 27 | 28 | _ "github.com/alibaba/ioc-golang/extension/autowire/allimpls/cli" 29 | _ "github.com/alibaba/ioc-golang/extension/autowire/common/cli" 30 | ) 31 | -------------------------------------------------------------------------------- /extension/registry/nacos/param.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package nacos 17 | 18 | import ( 19 | "github.com/nacos-group/nacos-sdk-go/v2/clients" 20 | "github.com/nacos-group/nacos-sdk-go/v2/vo" 21 | ) 22 | 23 | type Param struct { 24 | vo.NacosClientParam 25 | } 26 | 27 | func (c *Param) New(impl *NamingClient) (*NamingClient, error) { 28 | var err error 29 | impl.INamingClient, err = clients.NewNamingClient(c.NacosClientParam) 30 | if err != nil { 31 | return nil, err 32 | } 33 | return impl, nil 34 | } 35 | -------------------------------------------------------------------------------- /extension/state/redis/README.md: -------------------------------------------------------------------------------- 1 | # Redis 2 | 3 | ## 基本信息 4 | 5 | - Autowire 类型:normal 6 | - SDID:github.com/alibaba/ioc-golang/extension/state/redis.Redis 7 | - 参数来源:默认参数加载方式 8 | 9 | ## 参数说明: 10 | 11 | ```go 12 | type Config struct { 13 | Address string // redis 地址 14 | Password string // 密码 15 | DB string // 数据库序号 16 | } 17 | ``` 18 | 19 | ## 通过依赖注入 20 | 21 | 略 22 | 23 | ## 通过 API 获取 24 | 25 | ```go 26 | import ( 27 | normalRedis "github.com/alibaba/ioc-golang/extension/state/redis" 28 | ) 29 | 30 | redisImpl, err := normalRedis.GetRedis(&normalRedis.Config{ 31 | Address: "localhost:6379", 32 | }) 33 | if err != nil{ 34 | panic(err) 35 | } 36 | 37 | ``` 38 | 39 | 40 | ## 方法说明 41 | 42 | ```go 43 | type Redis interface { 44 | GetRawClient() *redis.Client // 获取 redis 数据库连接 45 | 46 | Set(key string, value interface{}, expiration time.Duration) (string, error) 47 | Get(key string) (string, error) 48 | HGetAll(key string) (map[string]string, error) 49 | } 50 | ``` 51 | -------------------------------------------------------------------------------- /extension/state/redis/param.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package redis 17 | 18 | import ( 19 | "strconv" 20 | 21 | "github.com/go-redis/redis" 22 | ) 23 | 24 | type Param struct { 25 | Address string 26 | Password string 27 | DB string 28 | } 29 | 30 | func (c *Param) New(impl *Redis) (*Redis, error) { 31 | if c.DB == "" { 32 | c.DB = "0" 33 | } 34 | dbInt, err := strconv.Atoi(c.DB) 35 | if err != nil { 36 | return impl, err 37 | } 38 | client := redis.NewClient(&redis.Options{ 39 | Addr: c.Address, 40 | Password: c.Password, 41 | DB: dbInt, 42 | }) 43 | _, err = client.Ping().Result() 44 | if err != nil { 45 | return impl, err 46 | } 47 | impl.client = client 48 | return impl, nil 49 | } 50 | -------------------------------------------------------------------------------- /iocli/Makefile: -------------------------------------------------------------------------------- 1 | tidy: 2 | go mod tidy 3 | 4 | test: 5 | go test ./... 6 | 7 | lint: tidy 8 | golangci-lint run 9 | 10 | build-all-platform: 11 | mkdir -p ./.release/linux-amd64 && GOOS=linux GOARCH=amd64 go build -o ./.release/linux-amd64 && cd .release && tar -czvf ./iocli-linux-amd64.tar.gz ./linux-amd64 12 | mkdir -p ./.release/linux-arm64 && GOOS=linux GOARCH=arm64 go build -o ./.release/linux-arm64 && cd .release && tar -czvf ./iocli-linux-arm64.tar.gz ./linux-arm64 13 | mkdir -p ./.release/darwin-amd64 && GOOS=darwin GOARCH=amd64 go build -o ./.release/darwin-amd64 && cd .release && tar -czvf ./iocli-darwin-amd64.tar.gz ./darwin-amd64 14 | mkdir -p ./.release/darwin-arm64 && GOOS=darwin GOARCH=arm64 go build -o ./.release/darwin-arm64 && cd .release && tar -czvf ./iocli-darwin-arm64.tar.gz ./darwin-arm64 15 | mkdir -p ./.release/windows-amd64 && GOOS=windows GOARCH=amd64 go build -o ./.release/windows-amd64 && cd .release && tar -czvf ./iocli-windows-amd64.tar.gz ./windows-amd64 16 | -------------------------------------------------------------------------------- /iocli/gen/generator/plugin/code_writer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package plugin 17 | 18 | type CodeWriter interface { 19 | Line(line string) 20 | Linef(line string, args ...interface{}) 21 | NeedImport(importPath string) string 22 | } 23 | -------------------------------------------------------------------------------- /iocli/gen/generator/plugin/common/util.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package common 17 | 18 | import "strings" 19 | 20 | func ToFirstCharLower(input string) string { 21 | return strings.ToLower(string(input[0])) + input[1:] 22 | } 23 | 24 | func ToFirstCharUpper(s string) string { 25 | if len(s) > 0 { 26 | return strings.ToUpper(string(s[0])) + s[1:] 27 | } 28 | return s 29 | } 30 | -------------------------------------------------------------------------------- /iocli/gen/generator/plugin/plugin.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package plugin 17 | 18 | import ( 19 | "sigs.k8s.io/controller-tools/pkg/loader" 20 | "sigs.k8s.io/controller-tools/pkg/markers" 21 | ) 22 | 23 | type CodeGeneratorPluginForOneStruct interface { 24 | Name() string 25 | Type() Type 26 | 27 | Init(info markers.TypeInfo) 28 | GenerateSDMetadataForOneStruct(root *loader.Package, c CodeWriter) 29 | GenerateInFileForOneStruct(root *loader.Package, c CodeWriter) 30 | } 31 | 32 | type Type int 33 | 34 | const ( 35 | AOP = Type(1) 36 | Autowire = Type(2) 37 | ) 38 | 39 | type CodeGeneratorPluginForPkg interface { 40 | GenerateCodeInPkg(c CodeWriter) 41 | } 42 | -------------------------------------------------------------------------------- /iocli/gen/generator/plugin/sorter.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package plugin 17 | 18 | type CodeGeneratorPluginForOneStructSorter []CodeGeneratorPluginForOneStruct 19 | 20 | func (m CodeGeneratorPluginForOneStructSorter) Len() int { 21 | return len(m) 22 | } 23 | 24 | func (m CodeGeneratorPluginForOneStructSorter) Less(i, j int) bool { 25 | return m[i].Name() < m[j].Name() 26 | } 27 | 28 | func (m CodeGeneratorPluginForOneStructSorter) Swap(i, j int) { 29 | m[i], m[j] = m[j], m[i] 30 | } 31 | -------------------------------------------------------------------------------- /iocli/gen/marker/getter.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package marker 17 | 18 | import "sigs.k8s.io/controller-tools/pkg/markers" 19 | 20 | type DefinitionGetter interface { 21 | GetMarkerDefinition() *markers.Definition 22 | } 23 | -------------------------------------------------------------------------------- /iocli/init/init-packr.go: -------------------------------------------------------------------------------- 1 | //go:build !skippackr 2 | // +build !skippackr 3 | 4 | // Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT. 5 | 6 | // You can use the "packr clean" command to clean up this, 7 | // and any other packr generated files. 8 | package init 9 | 10 | import _ "github.com/alibaba/ioc-golang/iocli/init/packrd" 11 | -------------------------------------------------------------------------------- /iocli/init/init_test.go: -------------------------------------------------------------------------------- 1 | package init 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "reflect" 7 | "testing" 8 | ) 9 | 10 | func Test_parseGOMod(t *testing.T) { 11 | type args struct { 12 | regex string 13 | src string 14 | name string 15 | } 16 | tests := []struct { 17 | name string 18 | args args 19 | want string 20 | }{ 21 | { 22 | name: "Test find()", 23 | args: args{ 24 | regex: `module\s+(?P[\S]+)`, 25 | src: "module github.com/alibaba/ioc-golang\n\ngo 1.17\n\nrequire (...)\n", 26 | name: "$name", 27 | }, 28 | want: "github.com/alibaba/ioc-golang", 29 | }, 30 | } 31 | for _, tt := range tests { 32 | t.Run(tt.name, func(t *testing.T) { 33 | if got := find(tt.args.regex, tt.args.src, tt.args.name); got != tt.want { 34 | t.Errorf("find() = %v, want %v", got, tt.want) 35 | } 36 | }) 37 | } 38 | } 39 | 40 | func Test_parseEVNecessary(t *testing.T) { 41 | type args struct { 42 | path string 43 | } 44 | 45 | ps := string(os.PathSeparator) 46 | goopath := fmt.Sprintf("%sUsers%sphotowey%sgo", ps, ps, ps) 47 | _ = os.Setenv("GOOPATH", goopath) 48 | _ = os.Setenv("HELLO_WORLD", "hello") 49 | 50 | tests := []struct { 51 | name string 52 | args args 53 | want string 54 | }{ 55 | { 56 | name: "Test parseEVNecessary()-1", 57 | args: args{ 58 | path: fmt.Sprintf("$GOOPATH%ssrc%sgithub.com%salibaba%s$HELLO_WORLD", ps, ps, ps, ps), 59 | }, 60 | want: fmt.Sprintf("%s%ssrc%sgithub.com%salibaba%shello", goopath, ps, ps, ps, ps), 61 | }, 62 | } 63 | for _, tt := range tests { 64 | t.Run(tt.name, func(t *testing.T) { 65 | if got := parseEVNecessary(tt.args.path); !reflect.DeepEqual(got, tt.want) { 66 | t.Errorf("parseEVNecessary() = %v, want %v", got, tt.want) 67 | } 68 | }) 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /iocli/init/option.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package init 17 | 18 | type Mod struct { 19 | Prefix string 20 | Name string 21 | } 22 | 23 | type Project struct { 24 | Path string 25 | Name string 26 | Mod *Mod 27 | } 28 | 29 | func newMod() *Mod { 30 | return &Mod{} 31 | } 32 | 33 | func newProject() *Project { 34 | return &Project{ 35 | Mod: newMod(), 36 | } 37 | } 38 | 39 | func initProject(opts ...Option) *Project { 40 | pt := newProject() 41 | for _, opt := range opts { 42 | opt(pt) 43 | } 44 | 45 | return pt 46 | } 47 | 48 | type Option func(*Project) 49 | 50 | func WithPath(path string) Option { 51 | return func(project *Project) { 52 | project.Path = path 53 | } 54 | } 55 | 56 | func WithName(Name string) Option { 57 | return func(project *Project) { 58 | project.Name = Name 59 | } 60 | } 61 | 62 | func WithModPrefix(prefix string) Option { 63 | return func(project *Project) { 64 | project.Mod.Prefix = prefix 65 | } 66 | } 67 | 68 | func WithModName(name string) Option { 69 | return func(project *Project) { 70 | project.Mod.Name = name 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /iocli/init/templates/.gitignore.tmpl: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | vendor/ 16 | 17 | .idea/ 18 | .vscode/ 19 | 20 | # Log 21 | logs/ 22 | **/*.log -------------------------------------------------------------------------------- /iocli/init/templates/README.md.tmpl: -------------------------------------------------------------------------------- 1 | # {{ .Name }} -------------------------------------------------------------------------------- /iocli/init/templates/cmd/app/service.go.tmpl: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | type Service interface { 8 | GetHelloString(string) string 9 | } 10 | 11 | // +ioc:autowire=true 12 | // +ioc:autowire:type=singleton 13 | 14 | type ServiceImpl1 struct { 15 | } 16 | 17 | func (s *ServiceImpl1) GetHelloString(name string) string { 18 | return fmt.Sprintf("This is ServiceImpl1, hello %s", name) 19 | } 20 | 21 | // +ioc:autowire=true 22 | // +ioc:autowire:type=singleton 23 | 24 | type ServiceImpl2 struct { 25 | } 26 | 27 | func (s *ServiceImpl2) GetHelloString(name string) string { 28 | return fmt.Sprintf("This is ServiceImpl2, hello %s", name) 29 | } 30 | 31 | // +ioc:autowire=true 32 | // +ioc:autowire:type=singleton 33 | 34 | type ServiceStruct struct { 35 | } 36 | 37 | func (s *ServiceStruct) GetString(name string) string { 38 | return fmt.Sprintf("This is ServiceStruct, hello %s", name) 39 | } 40 | 41 | // +ioc:autowire=true 42 | // +ioc:autowire:type=singleton 43 | // +ioc:autowire:alias=AliasStruct 44 | 45 | type AliasStruct struct { 46 | } 47 | 48 | func (s *AliasStruct) GetString(name string) string { 49 | return fmt.Sprintf("This is AliasStruct, hello %s", name) 50 | } 51 | 52 | // +ioc:autowire=true 53 | // +ioc:autowire:type=singleton 54 | // +ioc:autowire:alias=AliasStruct2 55 | 56 | type AliasStruct2 struct { 57 | } 58 | 59 | func (s *AliasStruct2) GetString(name string) string { 60 | return fmt.Sprintf("This is AliasStruct2, hello %s", name) 61 | } 62 | -------------------------------------------------------------------------------- /iocli/init/templates/configs/ioc_golang.yaml.tmpl: -------------------------------------------------------------------------------- 1 | autowire: 2 | singleton: 3 | {{ .Mod.Name }}/cmd/app.App: 4 | param: 5 | name: "{{ .Name }}" -------------------------------------------------------------------------------- /iocli/init/templates/docs/.Keep.tmpl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alibaba/IOC-golang/83a22c589d85756de38a138f5038537ada4d2775/iocli/init/templates/docs/.Keep.tmpl -------------------------------------------------------------------------------- /iocli/init/templates/go.mod.tmpl: -------------------------------------------------------------------------------- 1 | module {{ .Mod.Name }} 2 | 3 | go 1.17 4 | -------------------------------------------------------------------------------- /iocli/init/templates/internal/.Keep.tmpl: -------------------------------------------------------------------------------- 1 | package internal_test 2 | 3 | // 4 | // internal 5 | // 6 | -------------------------------------------------------------------------------- /iocli/init/templates/main.go.tmpl: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | App "{{ .Mod.Name }}/cmd/app" 5 | ) 6 | 7 | func main() { 8 | App.Run() 9 | } 10 | -------------------------------------------------------------------------------- /iocli/init/templates/pkg/.Keep.tmpl: -------------------------------------------------------------------------------- 1 | package pkg_test 2 | 3 | // 4 | // pkg 5 | // 6 | -------------------------------------------------------------------------------- /iocli/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | "log" 20 | 21 | _ "github.com/alibaba/ioc-golang/extension/imports/cli" 22 | _ "github.com/alibaba/ioc-golang/iocli/gen" 23 | _ "github.com/alibaba/ioc-golang/iocli/init" 24 | 25 | "github.com/alibaba/ioc-golang/iocli/root" 26 | ) 27 | 28 | func main() { 29 | if err := root.Cmd.Execute(); err != nil { 30 | log.Println(err) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /iocli/root/root.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package root 17 | 18 | import ( 19 | "fmt" 20 | 21 | "github.com/spf13/cobra" 22 | 23 | "github.com/alibaba/ioc-golang" 24 | ) 25 | 26 | var Cmd = &cobra.Command{ 27 | Use: "iocli", 28 | Run: func(cmd *cobra.Command, args []string) { 29 | if versionFlag { 30 | fmt.Printf("iocli version %s\n", ioc.Version) 31 | return 32 | } 33 | fmt.Println("hello") 34 | }, 35 | } 36 | 37 | var versionFlag bool 38 | 39 | func init() { 40 | Cmd.PersistentFlags().BoolVarP(&versionFlag, "version", "v", false, "Version of iocli") 41 | } 42 | -------------------------------------------------------------------------------- /iocli/root/root_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package root 17 | 18 | import ( 19 | "strings" 20 | "testing" 21 | "time" 22 | 23 | "github.com/stretchr/testify/assert" 24 | 25 | "github.com/alibaba/ioc-golang" 26 | "github.com/alibaba/ioc-golang/test/iocli_command" 27 | ) 28 | 29 | func TestPrintVersion(t *testing.T) { 30 | output, err := iocli_command.Run([]string{"--version"}, time.Second) 31 | assert.Nil(t, err) 32 | assert.True(t, strings.Contains(output, "iocli version "+ioc.Version)) 33 | } 34 | -------------------------------------------------------------------------------- /logger/logger.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package logger 17 | 18 | import ( 19 | "github.com/fatih/color" 20 | ) 21 | 22 | var disableLogs = false 23 | 24 | func Blue(format string, a ...interface{}) { 25 | if disableLogs { 26 | return 27 | } 28 | color.Blue(format, a...) 29 | } 30 | func Cyan(format string, a ...interface{}) { 31 | if disableLogs { 32 | return 33 | } 34 | color.Cyan(format, a...) 35 | } 36 | 37 | func Red(format string, a ...interface{}) { 38 | if disableLogs { 39 | return 40 | } 41 | color.Red(format, a...) 42 | } 43 | 44 | func Disable() { 45 | disableLogs = true 46 | } 47 | -------------------------------------------------------------------------------- /logger/logger_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package logger 17 | 18 | import ( 19 | "testing" 20 | 21 | "github.com/stretchr/testify/assert" 22 | ) 23 | 24 | func TestLoggerDisable(t *testing.T) { 25 | assert.True(t, !disableLogs) 26 | Disable() 27 | assert.True(t, disableLogs) 28 | } 29 | -------------------------------------------------------------------------------- /logo.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package ioc 17 | 18 | import ( 19 | "github.com/alibaba/ioc-golang/logger" 20 | ) 21 | 22 | var logo = " ___ ___ ____ _ \n |_ _| / _ \\ / ___| __ _ ___ | | __ _ _ __ __ _ \n | | | | | | | | _____ / _` | / _ \\ | | / _` | | '_ \\ / _` |\n | | | |_| | | |___ |_____| | (_| | | (_) | | | | (_| | | | | | | (_| |\n |___| \\___/ \\____| \\__, | \\___/ |_| \\__,_| |_| |_| \\__, |\n |___/ |___/ " 23 | 24 | func printLogo() { 25 | logger.Cyan(logo) 26 | } 27 | -------------------------------------------------------------------------------- /test/docker_compose/dockercompose.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package docker_compose 17 | 18 | import ( 19 | "log" 20 | "os/exec" 21 | "time" 22 | ) 23 | 24 | const waitTime = time.Second * 3 25 | 26 | func DockerComposeUp(path string, addtionalWaitTime time.Duration) error { 27 | out, err := exec.Command( 28 | "docker-compose", 29 | "-f", path, 30 | "up", "-d", 31 | "--remove-orphans").CombinedOutput() 32 | log.Printf("%s\n", string(out)) 33 | time.Sleep(waitTime) 34 | time.Sleep(addtionalWaitTime) 35 | return err 36 | } 37 | 38 | func DockerComposeDown(path string) error { 39 | out, err := exec.Command( 40 | "docker-compose", 41 | "-f", path, 42 | "down", "-v").CombinedOutput() 43 | log.Printf("%s\n", string(out)) 44 | time.Sleep(waitTime) 45 | return err 46 | } 47 | -------------------------------------------------------------------------------- /test/iocli_command/iocli_command.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package iocli_command 17 | 18 | import ( 19 | "bytes" 20 | "os/exec" 21 | "strings" 22 | "time" 23 | ) 24 | 25 | const whichCommand = "which" 26 | const iocliCommand = "iocli" 27 | 28 | func getCommandPath(commandName string) (string, error) { 29 | out, err := exec.Command(whichCommand, commandName).CombinedOutput() 30 | if err != nil { 31 | return "", err 32 | } 33 | return strings.TrimSuffix(string(out), "\n"), nil 34 | } 35 | 36 | func Run(command []string, timeout time.Duration) (string, error) { 37 | iocliBinPath, err := getCommandPath(iocliCommand) 38 | if err != nil { 39 | return "", err 40 | } 41 | 42 | var b bytes.Buffer 43 | cmd := exec.Command( 44 | iocliBinPath, 45 | command...) 46 | cmd.Stdout = &b 47 | cmd.Stderr = &b 48 | closeCh := make(chan struct{}) 49 | go func() { 50 | _ = cmd.Run() 51 | close(closeCh) 52 | }() 53 | 54 | after := time.After(timeout) 55 | select { 56 | case <-closeCh: 57 | case <-after: 58 | if cmd.Process != nil { 59 | _ = cmd.Process.Kill() 60 | } 61 | } 62 | return b.String(), nil 63 | } 64 | -------------------------------------------------------------------------------- /test/stress/aop/normal_app.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package aop 17 | 18 | import ( 19 | "testing" 20 | 21 | "github.com/stretchr/testify/assert" 22 | ) 23 | 24 | const reqString = "laurence" 25 | const expectString = "hello laurence" 26 | 27 | // +ioc:autowire=true 28 | // +ioc:autowire:type=normal 29 | 30 | type NormalApp struct { 31 | // inject main.ServiceImpl1 pointer to Service interface with proxy wrapper 32 | ServiceImpl1 Service `normal:"github.com/alibaba/ioc-golang/test/stress/aop.ServiceImpl1"` 33 | } 34 | 35 | func (s *NormalApp) RunTest(t *testing.T) { 36 | // test creat by API 37 | createByAPIService1, err := GetServiceImpl1IOCInterface() 38 | assert.Nil(t, err) 39 | assert.Equal(t, expectString, createByAPIService1.GetHelloString(reqString)) 40 | } 41 | -------------------------------------------------------------------------------- /test/stress/aop/recursive_app.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package aop 17 | 18 | import ( 19 | "testing" 20 | 21 | "github.com/stretchr/testify/assert" 22 | ) 23 | 24 | // +ioc:autowire=true 25 | // +ioc:autowire:type=singleton 26 | 27 | type RecursiveApp struct { 28 | ThisRecursiveApp 29 | // inject main.ServiceImpl1 pointer to Service interface with proxy wrapper 30 | ServiceImpl1 Service `normal:"github.com/alibaba/ioc-golang/test/stress/aop.ServiceImpl1"` 31 | counter int 32 | } 33 | 34 | func (s *RecursiveApp) Reset() { 35 | s.counter = 0 36 | } 37 | 38 | func (s *RecursiveApp) RunTest(t *testing.T) { 39 | if s.counter < 900 { 40 | s.counter++ 41 | s.This().RunTest(t) 42 | return 43 | } 44 | assert.Equal(t, expectString, s.ServiceImpl1.GetHelloString(reqString)) 45 | 46 | // test creat by API 47 | createByAPIService1, err := GetServiceImpl1IOCInterface() 48 | assert.Nil(t, err) 49 | assert.Equal(t, expectString, createByAPIService1.GetHelloString(reqString)) 50 | } 51 | -------------------------------------------------------------------------------- /test/stress/aop/service.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package aop 17 | 18 | import "fmt" 19 | 20 | type Service interface { 21 | GetHelloString(string) string 22 | } 23 | 24 | // +ioc:autowire=true 25 | // +ioc:autowire:type=singleton 26 | // +ioc:autowire:type=normal 27 | 28 | type ServiceImpl1 struct { 29 | } 30 | 31 | func (s *ServiceImpl1) GetHelloString(name string) string { 32 | return fmt.Sprintf("hello %s", name) 33 | } 34 | 35 | // +ioc:autowire=true 36 | // +ioc:autowire:type=singleton 37 | // +ioc:autowire:type=normal 38 | 39 | type ServiceImpl2 struct { 40 | } 41 | 42 | func (s *ServiceImpl2) GetHelloString(name string) string { 43 | return fmt.Sprintf("hello %s", name) 44 | } 45 | 46 | // +ioc:autowire=true 47 | // +ioc:autowire:type=singleton 48 | // +ioc:autowire:type=normal 49 | 50 | type ServiceStruct struct { 51 | } 52 | 53 | func (s *ServiceStruct) GetString(name string) string { 54 | return fmt.Sprintf("hello %s", name) 55 | } 56 | -------------------------------------------------------------------------------- /test/stress/autowire/normal_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | package autowire 16 | 17 | import ( 18 | "sync" 19 | "testing" 20 | 21 | "github.com/stretchr/testify/assert" 22 | ) 23 | 24 | func TestNormalAutowireConcurrent(t *testing.T) { 25 | wg := sync.WaitGroup{} 26 | for i := 0; i < 1000; i++ { 27 | wg.Add(1) 28 | go func() { 29 | defer wg.Done() 30 | for j := 0; j < 1000; j++ { 31 | normalApp, err := GetNormalAppIOCInterface() 32 | assert.Nil(t, err) 33 | normalApp.RunTest(t) 34 | } 35 | }() 36 | } 37 | wg.Wait() 38 | } 39 | -------------------------------------------------------------------------------- /test/stress/autowire/service.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package autowire 17 | 18 | import "fmt" 19 | 20 | type Service interface { 21 | GetHelloString(string) string 22 | } 23 | 24 | // +ioc:autowire=true 25 | // +ioc:autowire:type=singleton 26 | // +ioc:autowire:type=normal 27 | 28 | type ServiceImpl1 struct { 29 | } 30 | 31 | func (s *ServiceImpl1) GetHelloString(name string) string { 32 | return fmt.Sprintf("hello %s", name) 33 | } 34 | 35 | // +ioc:autowire=true 36 | // +ioc:autowire:type=singleton 37 | // +ioc:autowire:type=normal 38 | 39 | type ServiceImpl2 struct { 40 | } 41 | 42 | func (s *ServiceImpl2) GetHelloString(name string) string { 43 | return fmt.Sprintf("hello %s", name) 44 | } 45 | 46 | // +ioc:autowire=true 47 | // +ioc:autowire:type=singleton 48 | // +ioc:autowire:type=normal 49 | 50 | type ServiceStruct struct { 51 | } 52 | 53 | func (s *ServiceStruct) GetString(name string) string { 54 | return fmt.Sprintf("hello %s", name) 55 | } 56 | -------------------------------------------------------------------------------- /test/stress/autowire/singleton_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package autowire 17 | 18 | import ( 19 | "sync" 20 | "testing" 21 | 22 | "github.com/stretchr/testify/assert" 23 | ) 24 | 25 | func TestSingletonAutowireConcurrent(t *testing.T) { 26 | wg := sync.WaitGroup{} 27 | for i := 0; i < 1000; i++ { 28 | wg.Add(1) 29 | go func() { 30 | defer wg.Done() 31 | for j := 0; j < 1000; j++ { 32 | singletonApp, err := GetSingletonAppIOCInterfaceSingleton() 33 | assert.Nil(t, err) 34 | singletonApp.RunTest(t) 35 | } 36 | }() 37 | } 38 | wg.Wait() 39 | } 40 | -------------------------------------------------------------------------------- /test/stress/panic/panic_after_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, Alibaba Group; 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package panic 17 | 18 | import ( 19 | "testing" 20 | 21 | "github.com/stretchr/testify/assert" 22 | 23 | "github.com/alibaba/ioc-golang" 24 | "github.com/alibaba/ioc-golang/aop" 25 | ) 26 | 27 | const Name = "afterIsCalledAfterPanicTestInterceptor" 28 | 29 | func init() { 30 | // register a custom aop interceptor 31 | aop.RegisterAOP(aop.AOP{ 32 | Name: Name, 33 | InterceptorFactory: func() aop.Interceptor { 34 | i, _ := GetafterIsCalledAfterPanicTestInterceptorIOCInterfaceSingleton() 35 | return i 36 | }, 37 | }) 38 | } 39 | 40 | func TestAOPAfterWithPanic(t *testing.T) { 41 | assert.Nil(t, ioc.Load()) 42 | panicApp, err := GetPanicAfterCalledTestAppIOCInterfaceSingleton() 43 | assert.Nil(t, err) 44 | result := panicApp.RunWithPanic("test") 45 | assert.Equal(t, "test", result) 46 | 47 | i, _ := GetafterIsCalledAfterPanicTestInterceptorIOCInterfaceSingleton() 48 | // the invocation have two layers, the internal layer is called after panic 49 | // assert the internal layer is not skipped because of panic. 50 | assert.Equal(t, 2, i.GetAfterIsCalledNum()) 51 | } 52 | --------------------------------------------------------------------------------